用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这一步。