When a user requests a page, the server sends a piece of text to the client, including the printable characters and unprintable text. When the text arrives at the client, then, the visual markup characters are converted to visualized display by the client browser (such as IE), so when the user requests An ASPX page, the Asp.net Server Control on the page, the text will also be written to the returned text stream. In this chapter, we will describe this process.
The Asp.net Server Control provides four rendering Methods: render, renderchildren, rendercontents, and rendercontrol. Each of these four methods has an htmltextwriter type parameter. Htmltextwriter is used to write the markup characters and text into the inverted Asp.net Server Control Stream, Which is system. web. all the base classes of the UI namespace that Mark writers, including chtmltextwriter, html32textwriter, and xhtmltextwriter, are used to write elements, attributes, styles, and layout information for different tag types.
Since there are four rendering methods, what are their functions? What are the differences between them? The following describes how to generate a control:
Each page has a control tree that represents all the child controls on this page. The page control is the root of the Control tree. to generate the control tree, the page creates an htmltextwriter class instance, the corresponding stream is encapsulated in this instance, and the page passes the htmltextwriter object to rendercontrol. rendercontrol checks whether the visible attribute of the control is true. If it is true, rendercontrol calls the render method, the render method calls rendercontents. The rendercontents method is used to present the control content to the specified writer. If the control has child controls, the render method passes htmltextwriter to the renderchildren method, the renderchildren method is used to generate child controls for controls.
Let's take an example to illustrate this process.
Public class class1: webcontrol
{
Protected override void render (system. Web. UI. htmltextwriter writer)
{
Writer. Write ("renderchildren <br> ");
Base. Render (writer );
}
Protected override void renderchildren (system. Web. UI. htmltextwriter writer)
{
Writer. Write ("renderchildren <br> ");
Base. renderchildren (writer );
}
Public override void rendercontrol (system. Web. UI. htmltextwriter writer)
{
Writer. Write ("rendercontrol <br> ");
Base. rendercontrol (writer );
}
Protected override void rendercontents (system. Web. UI. htmltextwriter writer)
{
Writer. Write ("rendercontents <br> ");
Base. rendercontents (writer );
}
}
Figure 1 shows the effect (rendercontrol-> render-> rendercontents-> renderchildren ):
The parameter values of various htmltextwrite methods use three enumeration types: htmltextwritetag, htmltextwriteattribute, and htmltextwritestyle. I will not explain them in detail here. For example, Figure 2 shows the effect:
[Defaultproperty ("text")]
[Toolboxdata ("<{0}: webcustomcontrol1 runat = server ></ {0}: webcustomcontrol1>")]
Public class webcustomcontrol1: webcontrol
{
Protected override void render (htmltextwriter output)
{
//-----------------------------------------------------------------------------
// Method 1: Add + [attribute, styleattribute, begintag \ endtag]
// Addattriattribute, addstyleattribute before renderinintag and renderendtag
Output. beginrender (); // start render
Output. addattriter( htmltextwriterattribute. value, "this is input."); // use htmltextwriteattribute to enumerate
Output. addstyleattribute (htmltextwriterstyle. backgroundcolor, colortranslator. tohtml (color. greenyellow); // use htmltextwritestyle to enumerate
Output. renderbegintag (htmltextwritertag. Input); // use htmltextwritetag to enumerate
Output. renderendtag ();
Output. endrender (); // end render
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Method 2: Write + [attribute, styleattribute, begintag \ endtag]
// Writeattribute and writestyleattribute are between writebegintag and writeendtag.
Output. writebegintag ("input"); // directly marked name, without the htmltextwritetag Enumeration
Output. writeattribute ("value", "this is input too."); // directly use the attribute name, without the htmltextwriteattribute enumeration.
Output. Write ("style = \"");
Output. writestyleattribute ("background-color", colortranslator. tohtml (color. Lavender); // use style tags directly without htmltextwritestyle Enumeration
Output. Write ("\"");
Output. Write (htmltextwriter. tagrightchar); // Add the ">" mark
Output. writeendtag ("input ");
//-----------------------------------------------------------------------------
// The second method is applicable to friends who are familiar with HTML.
}
}
If you find anything inappropriate in this article, ask your friends for advice. Thank you first.