Programming of ASP. NET Server controls

Source: Internet
Author: User
Tags printable characters

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.

 
 
  1. public class Class1 : WebControl  
  2. {  
  3. protected override void Render  
  4. (System.Web.UI.HtmlTextWriter writer)  
  5. {  
  6. writer.Write("RenderChildren〈br 〉");  
  7. base.Render(writer);  
  8. }  
  9.  
  10. protected override void RenderChildren  
  11. (System.Web.UI.HtmlTextWriter writer)  
  12. {  
  13. writer.Write("RenderChildren〈br 〉");  
  14. base.RenderChildren(writer);  
  15. }  
  16.  
  17. public override void RenderControl  
  18. (System.Web.UI.HtmlTextWriter writer)  
  19. {  
  20. writer.Write("RenderControl〈br 〉");  
  21. base.RenderControl(writer);  
  22. }  
  23.  
  24. protected override void RenderContents  
  25. (System.Web.UI.HtmlTextWriter writer)  
  26. {  
  27. writer.Write("RenderContents〈br 〉");  
  28. base.RenderContents(writer);  
  29. }  

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:

 
 
  1. [DefaultProperty ("Text")]
  2. [ToolboxData ("<{ 0}: WebCustomControl1
  3. Runat = server> </{0}: WebCustomControl1> ")]
  4. PublicClass WebCustomControl1: WebControl
  5. {
  6. Protected override void Render
  7. (HtmlTextWriterOutput)
  8. {
  9. //----------------------------------------- 
  10. // The first method, throughAdd+ [Attribute,
  11. StyleAttribute, BeginTag \ EndTag]
  12. // Addattriattribute, AddStyleAttribute
  13. Before RenderBeginTag and RenderEndTag
  14. Output. BeginRender (); // start Render
  15. Output. Addattriter( HtmlTextWriterAttribute.
  16. Value,"This is input .");
  17. // Use HtmlTextWriteAttribute for enumeration
  18. Output. AddStyleAttribute (HtmlTextWriterStyle.
  19. BackgroundColor, ColorTranslator. ToHtml
  20. (Color. GreenYellow ));
  21. // Use HtmlTextWriteStyle Enumeration
  22. Output. RenderBeginTag (HtmlTextWriterTag. Input );
  23. // Use HtmlTextWriteTag for enumeration
  24. Output. RenderEndTag ();
  25. Output. EndRender (); // end Render
  26. //------------------------------------------ 
  27.  
  28. //------------------------------------------- 
  29. // Method 2: Write + [Attribute,
  30. StyleAttribute, BeginTag \ EndTag]
  31. // WriteAttribute and WriteStyleAttribute
  32. Between WriteBeginTag and WriteEndTag
  33. Output. WriteBeginTag ("Input");
  34. // Directly marked name, without the HtmlTextWriteTag Enumeration
  35. Output. WriteAttribute ("Value","This is input too .");
  36. // Directly use the attribute name, without the HtmlTextWriteAttribute Enumeration
  37. Output. Write ("Style = \"");
  38. Output. WriteStyleAttribute ("Background-color",
  39. ColorTranslator. ToHtml (Color. Lavender ));
  40. // Use style tags directly without HtmlTextWriteStyle Enumeration
  41. Output. Write ("\"");
  42. Output. Write (HtmlTextWriter. TagRightChar); // Add the ">" flag
  43. Output. WriteEndTag ("Input");
  44. //----------------------------------------------- 
  45.  
  46. // The second method is applicable to friends who are familiar with html.
  47. }
  48. }
  1. Shell functions in ASP. NET Environment
  2. Batch insert data to databases in ASP. NET
  3. ASP. NET sends data to webpages in Post Mode
  4. WEB Application Deployment in ASP. NET 2.0
  5. HttpWorkerRequest object in ASP. NET
  6. Introduction to ASP. net mvc Framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.