Composite Control
Inherit compositecontrol
Layout
Implement the inamingcontainer Interface
Inamingcontainer is a tag interface with no attribute Methods
Provides a protected virtual method to initialize the control.
Add control container. Controls. Add
Provides a protected virtual method to add the Container Control addcontainer (container con)
Use ensurechildcontrols to check bool childcontrolscreated to avoid multiple component creation.
After creating the child control, set childcontrolscreated to true;
Tagkey to determine the HTML elements to contain the entire custom control
This control is rewritten
Get (return htmltextwritertag. Table ;)
Rendercontrets (htmltextwriter writer)
Writer. renderbegintag ()
Rendercontainer (container, writer)
Rendercontainer (container, writer)
Writer. renderendtag ()
Sub-Control Properties
Public Virtual string paytext
{
Get
{
Ensurechildcontrols ();
Return paylabel. text;
}
Set
{
Ensurechildcontrols ();
Paylabel. Text = value;
}
}
Subcontrol triggers command event
Private Static readonly objectivecommandeventkey = new object ();
Public event commandeventhandle command
{
Add {events. addhandler (commandeventkey, value );}
Remove {event. removehandler (commandeventkey, value );}
}
Protected virtual void oncommand (commandeventarg E)
{
Commandeventhandler handler = event [commandeventkey] As commandeventhandler;
If (handler! = NULL) handler (this E );
Raisebubblerevent (this, e );
}
The commandeventargs event data class exposes the commandname and commandargument attributes to maintain the command name and command parameters.
Send commandeventargs to the raisebubblerevent method, which promotes the sub-control event to the parent control event.
Potected virtual void raisepostbackevent (string evnentargument)
{
...
Commandeventargs E = new commandeventargs (commandname, commandargument );
Oncommand (E );
}
The control class exposes the onbubbleevent method, which must be overwritten in accordance with the control.
Protected override bool onbubbleevent (Object source, eventargs ARGs)
First, check whether the command event is captured.
Commandeventargs Ce = Arg as commandeventargs;
If (Ce! = NULL & Ce. commandname = "validatecreditcard ")
This process can obtain the sub-control information, construct the event data class, and trigger the event
Compositecontrol provides the required features
1) rewrite the controls set.
2) Implement inaminginterface
3) override the databind Method
4) Implement the icompositecontroldesigneraccessor interface. This interface defines a recreatechildcontrols Method
5) before implementing render, when in design mode, you can override the render method to call the ensurechildcontrols method.
Override the controls set
Public override controlcollection Control
{
Get {
Ensurechildcontrol ();
Return base. controls;
}
}
Inamingcontainer sets a unique value for the clientid and uniqueid attributes
The child control inherits the namingcontainer attribute of the control class. This attribute points to the parent control that implements the inamingcontainer interface.