Analysis on the use of RenderContents in ASP. NET control development

Source: Internet
Author: User
Tags control label

Steps for rendering custom controls using RenderContents Based on ASP. NET controls:

Development of ASP. NET controls-use RenderContents 1. Start with inheriting WebControl

The second tutorial focuses on the use of the Render () method to present controls, but the controls inherited from the Control class have not yet played the role of the asp.net Control. as you know, web server controls are divided into HTML server controls (for example, <input id = "Button2" runat = "server" type = "button" value = "button"/>) and standard server controls (that is, <asp :.. id = "" runat = "server"/> controls)

Controls of HTML server controls are derived from the System. Web. UI. HtmlControls. HtmlControl class.

Controls of standard server controls are derived from the System. Web. UI. WebControls. WebControl class.

The HtmlControl and WebControl classes are derived from the System. Web. UI. Control class and extended.

Therefore, all server controls are inherited from System. web. UI. control class, that is, all server controls have the common properties of the Control class, such as the Visible and EnableViewState attributes. The HtmlControl class and WebControl class expand the System. web. UI. functions of the Control class, such

The HtmlControl class defines the methods, properties, and events common to all HTML server controls (for specific parameters, refer to MSDN)

The WebControl class defines the methods, properties, and events common to all standard server controls (for specific parameters, refer to MSDN)

For example, each standard control that inherits the WebControl class has properties that define the appearance and behavior, and then different controls can expand functions as needed.

 

Therefore, we recommend that you directly derive from the WebControl class instead of the Control class. What we do is not from the beginning. inheriting from the WebControl class can save us a lot of work.

Development of ASP. NET controls: RenderContents use 2. Rewrite WebControl class method, no longer Render ()

The WebControl class inherits the Control class. Of course, the Render method is available. In the WebControl class, the Render method is rewritten. The following code:

Example 1

 
 
  1. protected override void Render(HtmlTextWriter output)  
  2. {  
  3.  RenderBeginTag(output);  
  4.  RenderContents(output);  
  5.  RenderEndTag(output);  

Note that the RebderBeginTag method is not a method in the HtmlTextWriter class, but a method in the WebControl class, indicating to output HTML Tag header tags, such as <table ..... >, the RenderEndTag method outputs the end tag of the HTML Tag, for example, </table>. the RenderContents method in the middle is the Render method of the Control class. see the following RenderContents method definition.

Example 2

 
 
  1. Protected Override VoidRenderContents (HtmlTextWriter output ){
  2.  // Use the default logic to present the child control. Therefore, you must call the methods in the base class. 
  3.  Base. Render (output );
  4. }

Then let's look at the definition of the RenderBeginTag method.

Example 3

 
 
  1. Public Virtual VoidRenderBeginTag (HtmlTextWriter output)
  2. {
  3. // Add properties and styles of the render Control 
  4. // AddAttributesToRender is a method in the WebControl class. 
  5. AddAttributesToRender (output );
  6. // Display the control label 
  7. // If the label control is displayed, <span> 
  8. // Textbox Control rendering <input> 
  9. HtmlTextWriterTag tagKey = TagKey;
  10. If(TagKey! = HtmlTextWriterTag. Unknown)
  11. {
  12. Output. RenderBeginTag (tagKey );
  13. }
  14. Else 
  15. {
  16. Output. RenderBeginTag (This. TagName );
  17. }
  18. }

For example, if you want to output a table, you must define the <table> label header and then define the <tr>, <td>, the following describes the implementation of the Render method in the Control class, indicating that the Render method must complete all the tasks, including the attribute and style output of the label header tag <table> and <table>.

Example 4

 
 
  1. Protected Override VoidRender (HtmlTextWriter writer)
  2. {
  3. // Define attributes and styles for table labels 
  4. Writer. addattriter( HtmlTextWriterAttribute. Width,"287px");
  5. Writer. AddStyleAttribute (HtmlTextWriterStyle. BorderWidth,"0");
  6.  
  7. Writer. RenderBeginTag (HtmlTextWriterTag. Table );
  8.  
  9. Writer. RenderBeginTag (HtmlTextWriterTag. Tr );
  10. Writer. RenderBeginTag (HtmlTextWriterTag. Td );
  11. Writer. Write ("<Strong>"+ PaymentMethodText +"</Strong>");
  12. Writer. RenderEndTag ();
  13. Writer. RenderBeginTag (HtmlTextWriterTag. Td );
  14. Writer. addattriter( HtmlTextWriterAttribute. Name,"PaymentMethod");
  15. Writer. addattriter( HtmlTextWriterAttribute. Id,"PaymentMethod");
  16. Writer. AddStyleAttribute (HtmlTextWriterStyle. Width,"100%");
  17. Writer. RenderBeginTag (HtmlTextWriterTag. Select );
  18.  
  19. // The following code is omitted: 
  20. }

After the Render method is rewritten in the WebControl class, you can define the tag directly. The default value is <span>. You can modify the tag by rewriting the TagKey attribute, then, the AddAttributesToRender method defines the style and attribute for the label.

Example 5

 
 
  1. protected override HtmlTextWriterTag TagKey  
  2. {  
  3.   get { return HtmlTextWriterTag.Table; }  

Example 6

 
 
  1. Protected Override VoidAddAttributesToRender (HtmlTextWriter writer)
  2. {
  3. // Define attributes and styles for table labels 
  4. Writer. addattriter( HtmlTextWriterAttribute. Width,"287px");
  5. Writer. AddStyleAttribute (HtmlTextWriterStyle. BorderWidth,"0");
  6.  
  7. Base. AddAttributesToRender (writer );
  8.  
  9. }

Then rewrite the RenderContents method and compare the above Render method to achieve the same effect.

Example 7

 
 
  1. Protected Override VoidRenderContents (HtmlTextWriter writer)
  2. {
  3. // Note that no table label exists. Only its internal label is defined. 
  4. Writer. RenderBeginTag (HtmlTextWriterTag. Tr );
  5. Writer. RenderBeginTag (HtmlTextWriterTag. Td );
  6. Writer. Write (PaymentMethodText );
  7. Writer. RenderEndTag ();
  8. Writer. RenderBeginTag (HtmlTextWriterTag. Td );
  9. Writer. AddAttribute (HtmlTextWriterAttribute. Name, PaymentMethodSelectName );
  10. Writer. addattrid (HtmlTextWriterAttribute. Id, PaymentMethodSelectId );
  11. Writer. AddStyleAttribute (HtmlTextWriterStyle. Width,"100%");
  12. Writer. RenderBeginTag (HtmlTextWriterTag. Select );
  13. // The following content is omitted 
  14.  
  15. }

Therefore, the Render method after rewriting adds a label by default, and you can rewrite this label (default value: <span> ). it may be strange to everyone that the Render method can achieve the same effect. Is it necessary to rewrite the Render method and add a RenderContents method?

Differences between the RenderContents method and the RenderContents method in ASP. NET control development

When you inherit from the WebControl class and implement Sample 4 code in the RenderContents method (we will show it in the previous example), the following code is displayed: No, the Control ID is <span>
The label entered in the RenderContents method will become its internal label. let's take a look at the property panel of this control. You will see many attributes inherited from the WebControl class and set their attributes, that is, the attribute of setting the <span> label. this is probably the role of TagKey. To use the public attributes of the WebControl class. (I think so) not defined on the <table> label.
Example 8

 
 
  1. <Span id ="CreditCardForm5_1"> <Table style ="Border-width: 0 ;">
  2. <Tr>
  3. <Td> <strong> credit card type </strong> </td> <select name ="PaymentMethod"Id ="PaymentMethod"Style ="Width: 100% ;">
  4. ......

Assume that the <table> label in <span> is a sub-tag. In the RenderContents method, define the sub-tag of the control. If you only define the tag attribute, you only need to override the AddAttributesToRender method, you do not need to override the RenderContents method.

Next, let's talk about how to rewrite tags.

(1) rewrite the TagKey attribute. The label of the label control is rewritten below.

 
 
  1. public class Ch4Label: Label  
  2.   {  
  3.       protected override HtmlTextWriterTag TagKey  
  4.       {  
  5.           get { return HtmlTextWriterTag.Div; }  
  6.       }  
  7.   } 

(2) override the base class constructor. This method is applicable only after the Control class is inherited.

 
 
  1. public CreditCardForm5() : base(HtmlTextWriterTag.Table) { } 

Summary:

1. Control inherited from WebControl class

The main reason is that WebControl is more common than Control.

2. TagKey

The label of the control. The default value is <span>. You can override this attribute to modify or override the constructors of the WebControl class.

3. AddAttributesToRender Method

Add attributes and styles for tags

4. RenderContents Method

Present content in a tag

If the control is not complex, it can be inherited directly from the standard control (such as label), and then extended as needed. The AddAttributesToRender method can also be rewritten.

Change the default tag of the TagKey without rewriting the RenderContents method. If the control is complicated and not a single one, you need to output the internal content of the control in the RenderContents method.

In fact, the biggest difference is that by default, the WebControl class adds a tag for you to facilitate adding some public items of the WebControl class. If you rewrite the Render () method and discard the RenderContents method, you can enjoy all the attributes and methods that the WebControl class provides.

You are familiar with some common attributes of the WebControl class, and then modify the attributes more deeply.

I like to give things a full explanation slowly, or I feel uncomfortable, so I wrote it slowly, on the premise that I understand it. in this example, you can modify the code according to the second article. for errors, please indicate ^_^.

RenderContents for ASP. NET control development will introduce you here, hoping to help you understand the RenderContents basics of ASP. NET control development.

  1. Analysis of ASP. NET Server Control Processing and return data
  2. Analysis on capture and return events of ASP. NET Server controls
  3. Analysis of ASP. NET control development-based event processing
  4. Introduction to RenderContents for ASP. NET Server controls
  5. RenderContents application example of ASP. NET Server Control

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.