Analysis on the use of HtmlTextWriter class in ASP. NET control development skills

Source: Internet
Author: User

ASP. NET control development skills: How does one implement standard output labels using HtmlTextWriter class? Let's start with the following steps:

ASP. NET control development tips: Use HtmlTextWriter class to standardize the output tag 1.

Use built-in methods to output labels as much as possible

No one who has written controls will say that they have never used the Render and RenderContents methods. The key class is to use some methods of the HtmlTextWriter class to present html tags, but they are written differently, the results are the same. For example, output a drop-down box:

 
 
  1. Public ClassDemoControl: Control
  2. {
  3. Protected Override VoidRender (HtmlTextWriter writer)
  4. {
  5. // 1. Output html as an output string 
  6. /**//* 
  7. Writer. Write ("<select> "); 
  8. Writer. Write ("<option value = '0'> asp.net </option> "); 
  9. Writer. Write ("<option value = '1'> asp.net ajax </option> "); 
  10. Writer. Write ("</select> "); 
  11. */ 
  12.  
  13. // 2. Use the built-in recommendation method of. net to output html 
  14. Writer. RenderBeginTag (HtmlTextWriterTag. Select );
  15. Writer. addattriter( HtmlTextWriterAttribute. Value,"0");
  16. Writer. RenderBeginTag (HtmlTextWriterTag. Option );
  17. Writer. Write ("Asp.net");
  18. Writer. RenderEndTag ();
  19. Writer. addattriter( HtmlTextWriterAttribute. Value,"1");
  20. Writer. RenderBeginTag (HtmlTextWriterTag. Option );
  21. Writer. Write ("Asp.net ajax");
  22. Writer. RenderEndTag ();
  23. }
  24. }

When you are familiar with these methods, please try to use these methods

ASP. NET control development tips: Use HtmlTextWriter class to standardize the output tag 2.

Display tags in Segments

This method is equivalent to refactoring. When there are many tags displayed, do not write them down one by one. The segments will write the required items in each method and then combine them,
For example. Please present tags in segments to split functions. Then name the function starting with Render, which looks much clearer.

 
 
  1. protected override void Render(HtmlTextWriter writer)  
  2. {  
  3.     RenderTable(writer);  
  4. }  
  5.  
  6. private void RenderTable(HtmlTextWriter writer)  
  7. {  
  8.     writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");  
  9.     writer.RenderBeginTag(HtmlTextWriterTag.Table);  
  10.     RnderPaymentMethod(writer);  
  11.     RenderCreditCardNo(writer);  
  12.     RenderCardholderName(writer);  
  13.     RenderExpirationDate(writer);  
  14.     RenderSubmitButton(writer);  
  15.     writer.RenderEndTag();  

ASP. NET control development skills-use HtmlTextWriter class standard output tag 3.

Standard label ID name

Do not set the ID name to the header label of the Control to avoid overlapping names. You can assign the UniqueID attribute of the Control to the ID attribute, as are the child tags. It can be prefixed with the id of the parent tag and then named. The other advantage is that it can interact with the front-end and enhance flexibility.

 
 
  1. // Writer. addattriattribute (HtmlTextWriterAttribute. Id, "select1 "); 
  2. Writer. addattriter( HtmlTextWriterAttribute. Id,This. UniqueID );
  3. Writer. RenderBeginTag (HtmlTextWriterTag. Select );
  4. // Prefix with the id of the parent tag 
  5. Writer. addattriter( HtmlTextWriterAttribute. Id,This. UniqueID +"_ Asp");
  6. Writer. RenderBeginTag (HtmlTextWriterTag. Option );
  7. Writer. Write ("Asp.net");
  8. Writer. RenderEndTag ();
  9. Writer. RenderEndTag ();

ASP. NET control development tips: Use HtmlTextWriter class to standardize the output tag 4.

Determine output dynamic attributes

For example, if the Text attribute of TextBox is not set, the displayed label value attribute is not displayed, that is, the backend must judge, otherwise value = "" is cumbersome.

 
 
  1. string text = this.Text;  
  2. if (text.Length > 0)  
  3. {  
  4.     writer.AddAttribute(HtmlTextWriterAttribute.Value, text);  

For now, I want to add more. I don't want to write anything into it. Write separately later. Let's try again.

ASP. NET control development tips: Use the HtmlTextWriter class to standardize the implementation of the output tag. I will introduce it to you here, and hope to help you.

  1. Advantages of ASP. NET
  2. Analysis on ASP. NET Component Design
  3. Implementation of ASP. NET Component Design Code
  4. Development of ASP. NET controls-analysis of methods for modifying server controls
  5. Development of ASP. NET controls-how to trigger JavaScript scripts using UpdatePanel

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.