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 to the user through the client browser such as IE. Therefore, when the user requests An aspx page, the Asp.net Server Control on the page, it will also write the text into the returned text stream. In this chapter, we will describe this process and learn ASP. NET Server Control programming knowledge.
ASP. NET Server Control programming 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 the page. The page control is the root of the Control tree. to generate the control tree, an HtmlTextWriter class instance is created on the page; 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.
An example is provided to illustrate the programming process of ASP. NET Server controls.
- 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 1RenderControl-> Render-> RenderContents-> RenderChildren ):
In ASP. in NET Server Control Programming, parameter values of various HtmlTextWrite methods use three enumeration types: HtmlTextWriteTag, HtmlTextWriteAttribute, and HtmlTextWriteStyle, which are not described in detail here. For example, Figure 2 shows the effect:
- [DefaultProperty ("Text")]
- [ToolboxData ("<{ 0}: WebCustomControl1
- Runat = server> </{0}: WebCustomControl1> ")]
- PublicClass WebCustomControl1: WebControl
- {
- Protected override void Render
- (HtmlTextWriterOutput)
- {
- //-----------------------------------------
- // The first method, throughAdd+ [Attribute,
- StyleAttribute, BeginTag \ EndTag]
- // Addattriattribute, AddStyleAttribute
- Before RenderBeginTag and RenderEndTag
- Output. BeginRender (); // start Render
- Output. Addattriter( HtmlTextWriterAttribute.
- Value,"This is input .");
- // Use HtmlTextWriteAttribute for enumeration
- Output. AddStyleAttribute (HtmlTextWriterStyle.
- BackgroundColor, ColorTranslator. ToHtml
- (Color. GreenYellow ));
- // Use HtmlTextWriteStyle Enumeration
- Output. RenderBeginTag (HtmlTextWriterTag. Input );
- // Use HtmlTextWriteTag for enumeration
- Output. RenderEndTag ();
- Output. EndRender (); // end Render
- //------------------------------------------
-
- //-------------------------------------------
- // Method 2: Write + [Attribute,
- StyleAttribute, BeginTag \ EndTag]
- // WriteAttribute and WriteStyleAttribute
- 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 ">" flag
- Output. WriteEndTag ("Input");
- //-----------------------------------------------
-
- // The second method is applicable to friends who are familiar with html.
- }
- }
- Shell functions in ASP. NET Environment
- Batch insert data to databases in ASP. NET
- ASP. NET sends data to webpages in Post Mode
- WEB Application Deployment in ASP. NET 2.0
- HttpWorkerRequest object in ASP. NET
- Introduction to ASP. net mvc Framework