欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

js获取UserControl内容为拼html时提供方便_javascript技巧

程序员文章站 2022-03-19 19:25:18
...
今天看了老赵的文章可是怎么都调试不通过。

复制代码 代码如下:

[AjaxPro.AjaxMethod]
public string gethtml()
{

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

return s;

}

public class UcViewHelper 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 所以会造成

js获取UserControl内容为拼html时提供方便_javascript技巧

相关标签: UserControl