Introduction to the render method in ASP. NET

Source: Internet
Author: User
Runyl blog park I want to comment (1) font size: T | T

The control. Render method sends the server control content to the provided htmltextwriter object, which is written in the content that will be presented on the client. This article mainly introduces the render method in ASP. NET. Let's take a look.

AD: the 2013 global big data Technology Summit is selling tickets at a low price

 

Asp.netAll controls are from the system. Web. UI. control class. Three rendering-related methods are defined in the control class, which areRenderMethod, renderchildren method and rendercontrol method. The rendercontrol method is public. First, let's look at the implementation of these three methods:

  1. Public void rendercontrol (htmltextwriter writer)
  2. {
  3. // Determine whether the visible attribute is true. If yes, call the render method to render the control. Otherwise, the control is not displayed.
  4. If (visible)
  5. {
  6. Render (writer );
  7. }
  8. }
  9. Protected virtual void render (htmltextwriter writer)
  10. {
  11. // Write the code for rendering the control itself here
  12. .......
  13. // Call the renderchildren method to present the child control of the control
  14. Renderchildren (writer );
  15. }
  16. Protected virtual void renderchildren (htmltextwriter writer)
  17. {
  18. // Call the rendercontrol method in each sub-control to present the sub-control cyclically, and recursively render the control tree of the entire page
  19. Foreach (control C in controls)
  20. {
  21. C. rendercontrol (writer );
  22. }
  23. }

The rendercontrol method is used by external classes to generate controls. For example, a parent Control calls the rendercontrol method of a child control. the rendercontrol method only determines whether to display the control. If it is displayed, it calls the control's protected method render.

The render method is the core method for rendering controls. In a real-time custom control, we generally rewrite the render method to render the control. If the control is a container control, override the renderchildren method to present the child control.

All the server-side controls are from the system. web. UI. derived from webcontrol. webcontrol is derived from control. Therefore, webcontrol has the preceding three methods, but several methods are added. It divides the render method into three methods: renderbgegintag, rendercontents, renderendtag. the implementation code of render is as follows:

  1. Protected override void render (htmltextwriter writer)
  2. {
  3. // Present the start tag
  4. Renderbgegintag (writer );
  5. // Display the TAG content
  6. Rendercontents (writer );
  7. // Display the end tag
  8. Renderendtag (writer );
  9. }
  10. Public Virtual void renderbegintag (htmltextwriter writer)
  11. {
  12. // Call the addattributestorender method to add tag attributes
  13. Addattributestorender (writer );
  14. // Determine whether the displayed tag is a known tag;
  15. Htmltextwritertag tagkey = tagkey;
  16. If (tagkey! = Htmltextwritertag. Unknown)
  17. {
  18. Writer. renderbegintag (tagkey );
  19. }
  20. Else
  21. {
  22. // Unknown tag, the specified tagname attribute is used
  23. Writer. renderbegintag (this. tagname );
  24. }
  25. }
  26. Protected virtual void rendercontents (htmltextwriter writer)
  27. {
  28. // If You Want To render the child control, you must call the render method of the base class.
  29. Base. Render (writer );
  30. }

Looking at these methods, I don't think there is much need to break down the render method into three methods. This makes sense only for a control that presents a single label, composite Controls seem meaningless and increase complexity.

Summary:

1. if derived from control, the render () method should be reloaded to render the control. if the control is a container control, the base of the base class should be called in the render method. the renderchildren () method to present the child control.

2. if you derive from the webcontrol class, there are two situations: one is to use the tagkey attribute to generate the output HTML Tag. In this case, you should overload the rendercontents () method to present the control. the second is not to render the default HTML Tag generated with tagkey. In this case, the render () method should be reloaded to present the control, as shown in the following code:

  1. Protected virtual void render (htmltextwriter writer)
  2. {
  3. // Write the code for rendering the control itself here
  4. Addattributestorender (writer );
  5. Rendercontents (writer );
  6. }
  7. Protected virtual void rendercontents (htmltextwriter writer)
  8. {
  9. // Present the control code
  10. ....
  11. // If You Want To render the child control, you must call the render method of the base class.
  12. Base. Render (writer );
  13. }

3. If the control is derived from the webcontrol class and is a container control, you should call the base. Render () method in rendercontents to present the child control.

The other two methods will be introduced in subsequent articles. Hope to help you.

[Edit recommendations]

  1. Asp.net authentication method forms
  2. Design of large-scale high-performance ASP. NET System Architecture
  3. Use applications in iis7 using ASP. NET
  4. View changes in ASP. net mvc 3
  5. 10 most useful attributes of ASP. NET controls

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.