DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js獲取UserControl內容為拼html時提供方便
js獲取UserControl內容為拼html時提供方便
編輯:關於JavaScript     

今天看了老趙的文章可是怎麼都調試不通過。

復制代碼 代碼如下:
[AjaxPro.AjaxMethod]
public string gethtml()
{

UcViewHelper<UserControl> viewManager = new UcViewHelper<UserControl>();
UserControl control = viewManager.LoadViewControl("~/uc/giftoutmodel.ascx");
string s=viewManager.RenderView(control);

return s;

}

public class UcViewHelper<T> where T : UserControl
{
private MyPage m_pageHolder;
public T LoadViewControl(string path)
{
m_pageHolder = new MyPage();
return (T)m_pageHolder.LoadControl(path);
}
public string RenderView(T control)
{
StringWriter output = new StringWriter();

this.m_pageHolder.Controls.Add(control);
HttpContext.Current.Server.Execute(this.m_pageHolder, output, false);

return output.ToString();
}
}

class MyPage : Page
{
public override void VerifyRenderingInServerForm(Control control)
{
//if (control is GridView || control is UserControl)
//{
// return;
//}
//base.VerifyRenderingInServerForm(control);
}
}

測試通過。

如果:

復制代碼 代碼如下:
[AjaxPro.AjaxMethod]
public string gethtml()
{
string s = getString();
return s;

}
public string getString()
{
UserControl control = LoadControl("~/uc/giftoutmodel.ascx") as UserControl;
StringWriter tw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(tw);
control.RenderControl(writer);
return writer.InnerWriter.ToString();
}

public override void VerifyRenderingInServerForm(Control control)
{
// if (control is GridView || control is UserControl)
// {
// return;
//}
//base.VerifyRenderingInServerForm(control);
}

究其原因,原來是老趙的代碼 繼承了Page 然後使用VerifyRenderingInServerForm驗證,二我的代碼沒有繼承Page直接使用VerifyRenderingInServerForm 所以會造成

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved