Introduction to custom controls (1)

Source: Internet
Author: User

1. Overview of Custom Controls

Custom Controls often inherit from system. Web. UI. Control, system. Web. UI. webcontrols. webcontrol, system. Web. UI. webcontrols. compositecontrol.

The compositecontrol class inherits from webcontrol and webcontrol from control.

System. Web. UI. Control is the base class of all ASP. NET controls. Controls inherited from this base class are called fully custom controls.

All Web controls are inherited from system. Web. UI. webcontrols. webcontrol. webcontrol contains the start and end tags, so you can get more formatting options. The following two sectionsCode:

First, it inherits from control:

Public classFullyrendercontrol:Control{PublicFullyrendercontrol (){/// Todo: add the constructor logic here //}Protected override voidRender (HtmltextwriterWriter) {writer. Write (text );// Base. Render (writer );}Public StringText {Get;Set;}}

The following method inherits from webcontrol:

Public classFullyrendercontrol:Webcontrol{PublicFullyrendercontrol (){/// Todo: add the constructor logic here //}Protected override voidRendercontents (system. Web. UI.HtmltextwriterWriter) {writer. Write (text );}Public StringText {Get;Set;}}

After the custom controls of the two are referenced, the running results are referenced in the same way. However, when we look at the source file, we will find such a difference:

The first step is to inherit from the Control and generate such HTML:

The controls inherited from webcontrol are HTML:

Because the Control Generated from webcontrol always contains the start and end labels, it can obtain more formatting options, such as color and backcolor.

For example, literal inherits control, while label inherits webcontrol. From the attributes they have, we can see that this is a big problem.

2. Overview of common classes

Htmltextwriter. This class is often used in controls inherited from webcontrol.

In fact, I personally think that it is similar to Servlet's out. But it encapsulates more methods. I don't know if this is the case. My Understanding of JSP and servlet is limited to the ability to make logon interfaces.

He can write various things, such as write and writebreak. When adding attributes, you can also use these enumeration attributes, such:

Of course, CSS attributes can also be written to fit the web standards:

In the previous article, we saw that the HTML code generated by the custom control inherited from webcontrol is automatically added to <span>. Of course, you can also specify the added tag by yourself.

 Public class  Fullyrendercontrol : Webcontrol { Public Fullyrendercontrol (){ /// Todo: add the constructor logic here // }Protected override void Rendercontents (system. Web. UI. Htmltextwriter Writer) {writer. Write (text); writer. writeline (); writer. Write (text );} Protected override  Htmltextwritertag Tagkey { Get { Return  Htmltextwritertag . Div ;}} Public String Text { Get ; Set ;}}

3. Build a Composite Control

A compositecontrol is a combination of inheritance and compositecontrol. The combination control can use the original ASP. NET Server-side control. For example, you can combine a textbox with a requiredfieldvalidator control to form a non-empty textbox.

However, I seldom use this attribute. Long specified attributes are often generated by me .....................

In the method, the most common events are createchildcontrols and rendercontents. For more information, see msdn.

4. control status

ASP. NET Framework 2.0 introduces the concept of control status.

Previously, we had the concept of view State. Generally, view State is used to store control information to maintain the control state. However, the view is usually disabled to optimize performance. However, unlike the view status, the control status cannot be disabled. The control status is often used to store the most important information.

In this way, even if the view status is disabled, the control status is used to maintain important information about the control. When customizing a user control, we can specify the status of a control to store, for example, the following code:

 Public class  Fullyrendercontrol : Webcontrol { Public Fullyrendercontrol (){ /// Todo: add the constructor logic here // } Public String ControlState { Get ; Set ;} Protected override void Oninit ( Eventargs E) {page. registerrequirescontrolstate ( This ); // Register the control as a control in the persistent state.  Base . Oninit (E );} // Return the status of the saved Control  Protected override object Savecontrolstate (){ Return ControlState ;}Protected override void Loadcontrolstate ( Object Savedstate) {controlState = savedstate. tostring ();} Protected override void Rendercontents ( Htmltextwriter Writer) {writer. Write ( "Controlstatetext :" + ControlState); writer. writebreak ();}}

In this way, the control status is exposed, and the control status can be specified by the outside world. The Code is as follows:

 
Protected voidPage_load (ObjectSender,EventargsE ){This. Fullyrendercontrol1.controlstate ="Hello World";}

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.