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.