In the previous article, I wrote something about callback. I didn't know what to write this time. Because there are too many associations in all aspects, I still want to write it slowly. This time I will talk about WebControl.
1. Start from 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.
Figure 1
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.
2. Rewrite the WebControl method, instead of 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 protected override void Render (HtmlTextWriter output)
{
RenderBeginTag (output );
RenderContents (output );
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 HTML Tag, as shown in </table>. the RenderContents method in the middle is the Render method of the Control class. see the following RenderContents method definition.
Example 2 protected override void RenderContents (HtmlTextWriter output ){
// Use the default logic to present the child control. Therefore, you must call the methods in the base class.
Base. Render (output );
}
Then let's look at the definition of the RenderBeginTag method.
Example 3 public virtual void RenderBeginTag (HtmlTextWriter output)
{
// Add properties and styles of the render Control
// AddAttributesToRender is a method in the WebControl class.
AddAttributesToRender (output );
// Display the control label
// If the label control is displayed, <span>
// Render the textbox Control <input>
HtmlTextWriterTag tagKey = TagKey;
If (tagKey! = HtmlTextWriterTag. Unknown)
{
Output. RenderBeginTag (tagKey );
}
Else
{
Output. RenderBeginTag (this. TagName );
}
}
For example, to output a table, you must define the <table> label header and <tr>, <td>, the following describes the implementation of the Render method in the Control class, indicating that the Render method must complete all tasks, including the output of the attributes and styles of the <table> and <table> labels marked by the label header.
Example 4 protected override void Render (HtmlTextWriter writer)
{
// Define attributes and styles for table labels
Writer. AddAttribute (HtmlTextWriterAttribute. Width, "287px ");
Writer. AddStyleAttribute (HtmlTextWriterStyle. BorderWidth, "0 ");
Writer. RenderBeginTag (HtmlTextWriterTag. Table );
Writer. RenderBeginTag (HtmlTextWriterTag. Tr );
Writer. RenderBeginTag (HtmlTextWriterTag. Td );
Writer. Write ("<strong>" + PaymentMethodText + "</strong> ");
Writer. RenderEndTag ();
Writer. RenderBeginTag (HtmlTextWriterTag. Td );
Writer. AddAttribute (HtmlTextWriterAttribute. Name, "PaymentMethod ");
Writer. AddAttribute (HtmlTextWriterAttribute. Id, "PaymentMethod ");
Writer. AddStyleAttribute (HtmlTextWriterStyle. Width, "100% ");
Writer. RenderBeginTag (HtmlTextWriterTag. Select );
// The following code is omitted:
}
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 protected override HtmlTextWriterTag TagKey
{
Get {return HtmlTextWriterTag. Table ;}
}
Example 6 protected override void AddAttributesToRender (HtmlTextWriter writer)
{
// Define attributes and styles for table labels
Writer. AddAttribute (HtmlTextWriterAttribute. Width, "287px ");
Writer. AddStyleAttribute (HtmlTextWriterStyle. BorderWidth, "0 ");
Base. AddAttributesToRender (writer );
}
Then rewrite the RenderContents method and compare the above Render method to achieve the same effect.
Example 7 protected override void RenderContents (HtmlTextWriter writer)
{
// Note that no table label exists. Only its internal label is defined.
Writer. RenderBeginTag (HtmlTextWriterTag. Tr );
Writer. RenderBeginTag (HtmlTextWriterTag. Td );
Writer. Write (PaymentMethodText );
Writer. RenderEndTag ();
Writer. RenderBeginTag (HtmlTextWriterTag. Td );
Writer. AddAttribute (HtmlTextWriterAttribute. Name, PaymentMethodSelectName );
Writer. addattrid (HtmlTextWriterAttribute. Id, PaymentMethodSelectId );
Writer. AddStyleAttribute (HtmlTextWriterStyle. Width, "100% ");
Writer. RenderBeginTag (HtmlTextWriterTag. Select );
// The following content is omitted
}
Therefore, the Render method after rewriting adds a label by default, and you can override this label (<span> by default ). 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?
Iii. Differences between the Render method and RenderContents Method
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, and 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, setting the attribute of the <span> label. this is probably the role of TagKey. To use the public attributes of the WebControl class. (I think so) instead of being defined on the <table> label.
Example 8 <span id = "CreditCardForm5_1"> <table style = "border-width: 0;">
<Tr>
<Td> <strong> credit card type </strong> </td> <select name = "PaymentMethod" id = "PaymentMethod" style = "width: 100%;">
......
Assume that the <table> label in the <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.
Public class Ch4Label: Label
{
Protected override HtmlTextWriterTag TagKey
{
Get {return HtmlTextWriterTag. Div ;}
}
}
(2) override the base class constructor. This method is applicable only after the Control class is inherited.
Public CreditCardForm5 (): base (HtmlTextWriterTag. Table ){}
Summary:
1. The main reason that the Control inherits from the WebControl class is that the common thing of the WebControl class is better than the Control class.
2. The TagKey indicates the tag of the control, which is <span> by default. You can override this attribute to modify or override the WebControl constructor.
3. The AddAttributesToRender method adds attributes and styles to tags.
4. The RenderContents method presents the content in the 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 ^_^.
Reference: http://dev.yesky.com/msdn/37/2416037.shtml
This article is transferred from: http://www.cnblogs.com/Clingingboy/archive/2006/08/05/468694.html