When you want to output the html code of a control including the content (for example, exporting word or excel or directly outputting it, or returning it through httpxmlrequest), you must use the control's
Rendcontrol () method, such as gridview:
System. IO. StringWriter oStringWriter = new System. IO. StringWriter ();
System. Web. UI. HtmlTextWriter oHtmlTextWriter = new System. Web. UI. HtmlTextWriter (oStringWriter );
MyGrid. RenderControl (oHtmlTextWriter );
Response. Write (oStringWriter. ToString ());
Response. End ();
The page will create an HtmlTextWriter class instance. The corresponding stream is encapsulated in this instance, and the page will pass the HtmlTextWriter object to RenderControl. RenderControl will check whether the Visible attribute of the control is true, if it is true, RenderControl will call the Render method, while the Render method will call RenderContents. The RenderContents method is responsible for displaying the control content to the specified writer. If the control has a child control, the Render method passes HtmlTextWriter to the RenderChildren method. The RenderChildren method is used to generate child controls of the control.
We can think that the rendercontrol method first outputs the control content to htmlTextWriter, And because ostringwriter is associated with htmltextwriter, the content is stored in ostringwriter and then through response. write (ostringwriter. tostring () output.
Author Zheng wenliang