Control control style
Controls ultimately generate HTML code on the client, which can use a rich CSS style. Of course, CSS settings can be performed directly, but ASP.NET provides control developers to programmatically control the way.
If there is no special request for the style, directly inherit the style function of WebControl, if you need to modify or expand the inheritance style function, you need to deeply understand the background knowledge of the control style.
WebControl's style features are all encapsulated in the ControlStyle property (a property called Style System.Web.ui.WebControls.style). All style properties are subtribracuits for the ControlStyle property. The definition of WebControl's ControlStyle is:
PRIVATE _CONTENTSTYLE
......
Public Style ControlStyle
{
get
{
IF (_ContentStyle = = null)
{
_ContentStyle = CreateControlStyle ();
IF (iStrackingViewState)
{
(IStateManager) _Controlstyle) .trackviewstate ();
}
}
Return_ContentStyle;
}
}
We see that ControlStyle is a read-only attribute that is created during the first visit (this idea inherits the JIT scheme of the .NET).
So what is this CreateControlStyle?
Protected Virtual Style CreateControlStyle ()
{
Return New Style (ViewSatte);
}
It is also to read ViewState. This way, whether you are defined in the Control Declaration from the .aspx page to be reflected in the access.
As a control developer, you can customize the properties of the STYLE, for example, the MyTable control defines a TableControl type, adding tables supported by CellPadding / CellSpaceing.
There are three ways to program control attributes:
1. Cover the protected virtual function CreateControlStyle
2. Use the ApplyStyle (Style S) method to copy the custom property to the controlled yourself in your control.
3, MERGESTYLE (STYLE S) merge method to ControlStyle
We see that the control's ControlStyle attributes and other sub-properties are common statebags. Because the control style is called a configuration function that calls the transmission viewState. In addition, the sub-control is also stored in the same stateebag.
Composite control
First, the composite control is different from the user control because it is compiled, and the user control is deployed in text. But common points are class multiplexies to multiplex their features.
Composite controls contain multiple setup controls, multiplexed sub-controls. For example, when the composite control to be written contains TextBox, you don't have to implement the iPostBackDataHandler interface. Composite controls can send a child control class or a WebControl class, and the points of the compound is:
1. Reburden the CreateChildControl method to instantiate, initialize the child control, and add the child control into the control tree (add to the control tree of the Page to get the life cycle of the control). It is necessary to avoid executing business logic in the OnInit event.
2. Implement the System..web.ui.inamingContainer interface to create a new naming range under the composite control. InamingContainer is just a tag interface, allowing the frame to automatically implement the unique name of the child control. Why do you have to create a child control in the CreateChildControls method? In fact, this is to create sub-controls in order to create any child controls during the control life cycle, and you can use sub-controls to handle tasks such as booph data. To ensure that the child control is created before the code is created, the Controllei defined EnSureChildControls protection method checks the sub-control has been created, and if not created, you can call the CreateChildControls method to create. If the child control is not created before render, then the un created sub-control of Visible for TRUE is called EnSureChildControls by the default sub-control of TRUE.
Composition can be reused, but it also brings performance loss (such as sub-control instantiation, etc.). Therefore, you need to trade between performance and ease of use, either composite control, or simply write completely generated controls.
How does the composite control view status work?
Control builds a status of tracking, saving, and recovering sub-controls.
In the Start Tracking View status phase, Control sequentially calls the TRACKVIERSTATE method of the controls in the Controls collection, track the status of the sub-control. If the child control is added to the Controls in the open state in the parent control, the TrackViewState method is called when adding to a collection.
In the save view status phase, Control first calls the SaveViewsate method, by default, the ViewState dictionary SaveViewState is first called. And save the returned object, as the first part of the control view; Next, Control calls SaveViewState, if the returned child control is not empty, then the number of the sub-control is saved in two arraylist in two ArrayList State, used to serialize.
In the loading view status phase, Control first calls the LoadViewState method to restore the first part of the previously saved status, next to the Controls to access the Controls collection, load the remaining status into the sub-control, which is generally composed of ArrayList by the number and saving, so Restore the turntable of the control and its sub-control. If you have not created a child control at this stage, then save the status of the sub-control, then use it later until the child control is created, load it to the child control.
Event bubbling
ASP.NET Page Framework provides a technology called "event bubbling" that allows sub-controls to spread events along their tolerance hierarchy. Event bubbles allow events to raise events in the control hierarchy, and allows event handler to attach the event handler to the control of the original control and the control of the public bubble.
For example: Data Binding Controls (Repeater, DataList, and DataGrid) use event bubbling to open a sub-control (in the project template) command event to a top event. Although the ASP.NET server control in the .NET framework uses the event to bubbit the event (the event data class is derived from the CommandEventArgs), any event defined on the server control can bubbber.
The control can participate in the event bubbling by two methods inherited from the base class system.web.ui.control. These two methods are: OnbubbleEvent and Raisebubbleevent. The following code snippet shows the signature of these methods.
Protected Virtual Bool OnbubbleEvent
Object Source, Eventargs Args
);
Protected void raisebubbleevent
Object Source,
Eventargs args
);
The implementation of the RaisebubbleEvent is provided by Control and cannot be rewritten. RaisebubbleEvent sends event data to the parent of the control along the hierarchy. To handle or trigger the bubble event, the control must override the OnbubbleEvent method.
Make the controls of the event to perform one of the following three operations.
1. The control does not perform any operation, at which time the event is automatically bubbled to its parent.
2, the control is handled and continues to bubbber events. To achieve this, the control must override OnbubbleEvent and call RaisebubbleEvent from OnbubbleEvent. The following code snippet (Example of Template Data Binding Control) makes events after checking the type of event parameters.
Protected Override Bool OnbubbleEvent (Object Source, Eventargs E) {
IF (e is commandeventargs) {
TemplatedListCommandEventArgs args =
New TemplatedListCommandeventArgs (this, source, (commandEventArgs);
RaisebubbleEvent (this, args);
Return True;
}
Return False;
}
3, the control stops the event bubbling and triggered and / or handling the event. Starting events require calling to schedule events to listeners. To trigger the bubbling event, the control must override OnbubbleEvent to call the OneventName method that triggers this bubbling event. Controls that cause bubbling events typically open the bubbling events as top events.
Protected Override Bool OnbubbleEvent (Object Source, Eventargs E) {
Bool handled = false;
IF (e is templatedListCommandEventArgs) {
TemplatedListCommandeventArgs CE = (TemplatedListCommandEventArgs) E;
OnItemCommand (CE);
Handled = True;
}
Return Handled;
}
Temption control
With analog control, control developers can specify all or part of the UI by template. The template is part of the page syntax, which can include static HTML (HTML syntax expressed controls actually a sub-control, but belonging to the LiteralControl control) and the server controls for other literary documents. The template function allows the control data to be separated from it. The template control itself does not provide a user interface (UI). The UI of the control is provided by the page developer through the inline template, which allows page developers to customize the UI of the control. By using templates, the control generates different from the UI, but this UI ability is primarily generating page elements. For example, the REPEATER control, etc.
To support templateization, the control must implement the Itemplate interface. The page parser parses the text in the template tag and generates a parsing tree to represent the contents of the template, just like the entire Page. Support template control development needs:
1. Implement the System.Web.ui.inamingContainer interface. This is a tag interface without any method. It can create a new naming range under your control, so that the unique identifier is in the name tree.
Public Class TemplatedFirstControl: Control, InamingContainer {...}
2. Apply Parsechildrenattribute to the control and pass True as a parameter. This can indicate how the page analyzer analyzes the template properties tag when using the control on the ASP.NET page. Step 3 shows how to define a template properties. Note If your control is derived from WebControl, you do not need to apply ParsechIldrenattribute because WebControl has been tagged with this property. [Childrenasproperties = true]]
Public Class TemplatedFirstControl: Control, InamingContainer {...}
3, define one or more properties of the System.Web.ui.Itemplate type. Itemplate has a method Instantiatein that creates controls using the templates provided on the onllide. There is no need to implement instantiatein methods; the ASP.NET page framework provides this implementation. The itemplate property must have a metadata property of the System.Web.ui.TemplateContainerattribute, which inamingContainer control will have an instantiation template. This is explained in step 4. A template attribute is defined as shown below.
[TemplateContainer (TypeOf (FirstTemplateContainer)]]]]
Public itemplate firsttemplate {...}
4. The parameters for TemplateContainerattribute are the type of container controls you want to instantiate the template. Container controls are independent of template controls that are being created. The reason for having a logical container is that template controls typically have a template that requires repeated instantiation using different data. Have a different container control different from the root template control, enabling multiple such examples possible. The logical container is an instant inamingContainer for the template. This relationship is introduced in more detail in the development template data binding control.
Note that the container control itself must implement inamingContainer because it has a sub-control that needs to be named on the page. However, the container is merely a container, and the template content that needs to be explained is not controlled.
Public Class FirstTemplateContainer: Control, InamingContainer {...}
5, rewrite the CreateChildControls method to create a child control in the template. This is done by three steps.
Instantiate the template container.
Call the instantiatein method of the template property and pass the container as a parameter to it. Instantiatein method (declared in the Itemplate interface) instantiate the element of the template as a child control of the template container. There is no need to implement instantiatein methods; the ASP.NET page framework provides this implementation.
Add an example of a template container to your template control for a Controls collection.
The following code snippet illustrates the implementation of CreateChildControls.
Private control mytemplatecontainer;
Protected Override Void CreateChildControls ()
{
IF (FirstTemplate! = null)
{
MyTemplateContainer = New FirstTemplateContainer (this);
FirstTemplate.instantiatein (MyTemplateContainer);
Controls.add (MyTemplateContainer);
}
Else
{
Controls.add (New LitralControl (Text " DateTime);
}
5, override the onDatabase method inherited from Control to call the EnSureChildControls method. This ensures that the sub-controls in the template are created before the page frame attempts to calculate any data binding expressions in the template. You must also call the ONDATABINDING method for the base class to ensure that the registered event handler is called.
Protected Override Void ONDATABINDING (Eventargs E) {
EnsureChildControls ();
Base.ondatabinding (e);
}
7. In step 5, the logic is repeated in the CreateChildControls method to instantiate a template for each template attribute of the control.
We see, usually, the Container we specified in the template actually requires our control developers to define themselves. In fact, if you do not repeat the generator control, INAMINGCONTAINER can also be implemented. But start reminding needs to implement this interface. If you want to support data binding in the control, the template container should be binded by one or more attributes. Usually template classes are implemented as internal private classes of control classes.
Controls can repeat a template (as long as they can be in different container instances), and therefore, the template should not contain any control instances as a member variable, because when the template is instantiated, the member variable can be used in new The value is overwritten.