2010-02-13 54 views
0

我在ProcessRequest(HttpContext context)中有一組代碼(如下所述)在通用處理程序.ashx文件中。從HttpHandler呈現HtmlGenericControl

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,我想holder控制HTML,並通過context.Respose.Write()寫。

請幫助我解決這個問題。
在此先感謝

穆奈姆

回答

2

我已經想通自己的出路。

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