用HttpContext获得Server对象,然后执行Execute方法,看代码:
public string RenderView(UserControl control)
{
Page page=new Page();
Form form=new Form();
page.Controls.Add(form);
StringWriter output = new StringWriter();
form.Controls.Add(control);
HttpContext.Current.Server.Execute(page, output, false);
return output.ToString();
}
{
Page page=new Page();
Form form=new Form();
page.Controls.Add(form);
StringWriter output = new StringWriter();
form.Controls.Add(control);
HttpContext.Current.Server.Execute(page, output, false);
return output.ToString();
}
如果是不需要在Form中的控件,我们可以省去添加Form这一步。
本文介绍了一种通过HttpContext对象的Server属性调用Execute方法来渲染UserControl的方法。具体实现是创建一个Page对象,并将需要渲染的UserControl添加到其Form控件中,最后通过StringWriter捕获渲染结果。
704

被折叠的 条评论
为什么被折叠?



