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:
- Public ClassDemoControl: Control
- {
- Protected Override VoidRender (HtmlTextWriter writer)
- {
- // 1. Output html as an output string
- /**//*
- Writer. Write ("<select> ");
- Writer. Write ("<option value = '0'> asp.net </option> ");
- Writer. Write ("<option value = '1'> asp.net ajax </option> ");
- Writer. Write ("</select> ");
- */
-
- // 2. Use the built-in recommendation method of. net to output html
- Writer. RenderBeginTag (HtmlTextWriterTag. Select );
- Writer. addattriter( HtmlTextWriterAttribute. Value,"0");
- Writer. RenderBeginTag (HtmlTextWriterTag. Option );
- Writer. Write ("Asp.net");
- Writer. RenderEndTag ();
- Writer. addattriter( HtmlTextWriterAttribute. Value,"1");
- Writer. RenderBeginTag (HtmlTextWriterTag. Option );
- Writer. Write ("Asp.net ajax");
- Writer. RenderEndTag ();
- }
- }
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.
- protected override void Render(HtmlTextWriter writer)
- {
- RenderTable(writer);
- }
-
- private void RenderTable(HtmlTextWriter writer)
- {
- writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
- writer.RenderBeginTag(HtmlTextWriterTag.Table);
- RnderPaymentMethod(writer);
- RenderCreditCardNo(writer);
- RenderCardholderName(writer);
- RenderExpirationDate(writer);
- RenderSubmitButton(writer);
- 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.
- // Writer. addattriattribute (HtmlTextWriterAttribute. Id, "select1 ");
- Writer. addattriter( HtmlTextWriterAttribute. Id,This. UniqueID );
- Writer. RenderBeginTag (HtmlTextWriterTag. Select );
- // Prefix with the id of the parent tag
- Writer. addattriter( HtmlTextWriterAttribute. Id,This. UniqueID +"_ Asp");
- Writer. RenderBeginTag (HtmlTextWriterTag. Option );
- Writer. Write ("Asp.net");
- Writer. RenderEndTag ();
- 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.
- string text = this.Text;
- if (text.Length > 0)
- {
- 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.
- Advantages of ASP. NET
- Analysis on ASP. NET Component Design
- Implementation of ASP. NET Component Design Code
- Development of ASP. NET controls-analysis of methods for modifying server controls
- Development of ASP. NET controls-how to trigger JavaScript scripts using UpdatePanel