把UserControl通过代码控制输入HTML

用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();
}

如果是不需要在Form中的控件,我们可以省去添加Form这一步。

原文地址:https://www.cnblogs.com/format/p/1670946.html