Control rendering is the process of writing markup text to the HTTP output stream. The server sends the generated markup text to the client through the HTTP output, which is displayed through the conversion of the client browser to a visual element. With control rendering, developers can enter HTML tags, script code, CSS stylesheet, and so on to the client browser. There are two main ways to implement the server control rendering: One is the Render method and the other is the WebControl rendercontents method. This article focuses on the use of the control class render method to implement the application of controls rendering.
Using the HtmlTextWriter class
The Render method of the control class is primarily used to implement the rendering of controls, whose declaration code is as follows:
protected virtual void Render(HtmlTextWriter output)
As shown in the code above, the parameter of the Render method is a HtmlTextWriter type. In order to better apply the Render method, the reader should first understand the HtmlTextWriter class and its related content.
According to MSDN2005 's description, the HtmlTextWriter class is used to write markup characters and text to the ASP. NET server control output stream. This class provides the formatting features that ASP.net server controls use when rendering markup to clients. To implement the functionality of a class, the HtmlTextWriter class defines multiple fields, properties, and methods. Due to the large number of member objects, this article only selected some of the common members to explain, and will also introduce some ASP.net 2.0 new members.
Common member objects include:
· AddAttribute method
For the HtmlTextWriter object, add the specified tag properties and values to its opening tag by the element created by subsequent calls to the RenderBeginTag method.
· AddStyleAttribute method
For the HtmlTextWriter object, add the tag style attribute to its opening tag by the element created by subsequent calls to the RenderBeginTag method.
· Write method
Writes the specified data type along with any pending tab spacing to the output stream.
· WriteAttribute method
Writes a tag property and its value to the output stream.
· WriteBeginTag method
Any tab spacing and the start tag of the specified markup element are written to the output stream.
· Writeendtag method
Writes any tab spacing and closing tags for the specified markup element.
· Encoding Property
Gets the encoding that the HtmlTextWriter object uses to write content to the page.
· Indent Property
Gets or sets the number of tab positions that are used to indent the start position of each line of markup.