从HttpHandler渲染HtmlGenericControl

时间:2010-02-13 09:03:31

标签: c# .net asp.net httphandler

我在ProcessRequest(HttpContext context)的通用处理程序.ashx文件中有一组代码(如下所示)。

HtmlGenericControl holder = new HtmlGenericControl("div");
//Set holder's properties and customization
HtmlGenericControl button1 = new HtmlGenericControl("input");
//Set button1's properties and customization
holder.Controls.Add(button1);
HtmlGenericControl button2 = new HtmlGenericControl("input");
//Set button2's properties and customization
holder.Controls.Add(button2);
HtmlGenericControl button2 = new HtmlGenericControl("input");
//Set button2's properties and customization
holder.Controls.Add(button2);

现在,当我在HttpHander时,我希望获得HTML holder控件,并通过context.Respose.Write()写下来。

请帮我解决这个问题 提前致谢

穆奈姆

1 个答案:

答案 0 :(得分:2)

我自己已经找到了方法。

using (StringWriter stringWriter = new StringWriter())
{
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
    holder.RenderControl(htmlTextWriter);
    HttpContext.Response.Write(stringWriter.ToString());
    htmlTextWriter.Close();
    stringWriter.Close();
}