//利用Httphandler、UserControl 输出HTML片段
//ViewManager类
public class ViewManager<T> where T : UserControl
{
private Page m_pageHolder;
public T LoadViewControl(string path)
{
this.m_pageHolder = new Page();
return (T)this.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();
}
}
//Handler 使用
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
ViewManager<AreaList> viewManager = new ViewManager<AreaList>();
AreaList control = viewManager.LoadViewControl("~/HttpHandler/AreaList.ascx");
context.Response.Write(viewManager.RenderView(control));
}
转载于:https://www.cnblogs.com/wangyhua/p/4050569.html