Some Learning Experiences on composite controls in Asp.net

Source: Internet
Author: User
In. NET 2.0, system. Web. UI. webcontrols. compositecontrol makes the composite control easier to use.
In the past, the most common code for composite controls was public class labeltextbox: webcontrol, inamingcontainer.
{
Public String text {
Get {
Object o = viewstate ["text"];
If (O = NULL)
Return string. empty;
Return (string) O;
}
Set {viewstate ["text"] = value ;}
}
Public String title {
Get {
Object o = viewstate ["title"];
If (O = NULL)
Return string. empty;
Return (string) O;
}
Set {viewstate ["title"] = value ;}
}
Protected override void createchildcontrols ()
{
Controls. Clear ();
Createcontrolhierarchy ();
Clearchildviewstate ();
}
Protected virtual void createcontrolhierarchy ()
{
Textbox T = new Textbox ();
Label L = new label ();
T. Text = text;
L. Text = title;
Controls. Add (L );
Controls. Add (t );
}
}

The point is that this public class labeltextbox: webcontrol, inamingcontainer
Inherits the webcontrol class and implements the imamingcontainer interface.
However, this composite control design interface is unfriendly during use. In 2.0, a system. Web. UI. webcontrols. compositecontrol is added.
In this way, there will be no design obstacles when the control is applied. The Code is as follows: using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

Namespace compositecontrol
{
[Defaultproperty ("text")]
[Toolboxdata ("<{0}: labeltextbox runat = Server> </{0}: labeltextbox>")]
Public class labeltextbox: system. Web. UI. webcontrols. compositecontrol
{
Public String text
{
Get
{
Object o = viewstate ["text"];
If (O = NULL)
Return string. empty;
Return (string) O;
}
Set
{
Viewstate ["text"] = value;
}

}
Public String title11
{
Get
{
Object o = viewstate ["title"];
If (O = NULL)
Return string. empty;
Return (string) O;
}
Set
{
Viewstate ["title"] = value;
}
}

Protected override void createchildcontrols ()
{
Controls. Clear ();
Createcontrolhierarchy ();
Clearchildviewstate ();
}
Protected virtual void createcontrolhierarchy ()
{
Textbox T = new Textbox ();
Label L = new label ();
T. Text = text;
L. Text = title11;
Controls. Add (L );
Controls. Add (t );
}

}
}

This is the case. You can try it.

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.