Development of templated data binding controls)

Source: Internet
Author: User
Using the ASP. NET data binding syntax, you can easily bind control attributes to a data item (or expression ). This section deals with more complex control development solutions. The control has templated attributes bound to a data source of the collection type (system. Collections. icollection or system. Collections. ienumerable. The template allows page developers to customize the data representation bound to the control. The repeater and datalist controls are examples of templated data binding controls.
For an overview of data binding on the ASP. NET page, the templated data binding control has one or more attributes of the icollection or ienumerable type and itemplate type. The container of these template attributes defines the attributes (usually named dataitem) to which data is bound ). The control implements its data binding logic in the databind method inherited from the control. It overwrites the createchildcontrols method when sending back to recreate the child control hierarchy. These steps will be explained in more detail in the following discussion.

Define the control that implements the system. Web. UI. inamingcontainer interface.
Public ClassTemplatedlist: webcontrol, inamingcontainer {}

Defines the attributes of the system. Web. UI. itemplate type.
[Templatecontainer ( Typeof (Templatedlistitem)]
Public   Virtual Itemplate itemtemplate {
Get {
Return Itemtemplate;
}
Set {
Itemtemplate = Value;
}
}

The logical container of the template (specified in the templatecontainerattribute attribute) must have the attribute to bind data. According to the conventions, this attribute is named dataitem. For more information about the logical container of template properties, see develop templated controls. The following example defines the container of the template attribute.
Public   Class Templatedlistitem: tablerow, inamingcontainer {
Private   Object Dataitem;
Public   Virtual   Object Dataitem {
Get {
Return Dataitem;
}
Set {
Dataitem = Value;
}
}

Override the databind method (inherited from control) to provide the data binding logic. This operation must include the following steps:
Call the ondatabinding method of the base class to call the processing of the data binding expression calculated for your control Program (Appended by this page ).
Clear the controls set.
Clears the viewstate of the child control.
Create a child control using the data source.
Sends a signal to the ASP. NET page framework to track the viewstate of the control.
Below Code These steps will be performed. Createchildcontrolshierarchy is a helper method used to create child controls. For more information, see 5 .
Public   Override   Void Databind (){
// Controls with a data-source property perform their
// Custom Data Binding by overriding databind
// Evaluate any data-binding expressions on the Control
// Itself.
Base . Ondatabinding (eventargs. Empty );

//Reset the control's state.
Controls. Clear ();
Clearchildviewstate ();

//Create the control hierarchy using the data source.
Createcontrolhierarchy (True);
Childcontrolscreated= True;

Trackviewstate ();
}

Override createchildcontrols to recreate the child control in the resend solution. This involves clearing the controls set and using the view status instead of the data source to create the control hierarchy. The actual creation of the Child control is hidden in the step5In the createcontrolhierarchy method described in.
Protected Override VoidCreatechildcontrols (){
Controls. Clear ();

If (Viewstate [ " Itemcount " ] ! =   Null ){
// Create the control hierarchy using the view state,
// Not the data source.
Createcontrolhierarchy ( False );
}
}

Define a data source with null elements, and use this data source instead of the actual data source when creating a control hierarchy during sending back. Procedure3And steps4Use the data source and saved view status to create the control hierarchy. The virtual data source allows the control to implement a single code path for the public elements in these two steps.
Note this step (step5) Describes the implementation information used by the. NET Framework to bind data to ASP. NET controls. The dummydatasource class and createcontrolhierarchy methods shown in the following code snippet are not in. NET Framework and must be defined by control developers. Do not implement these elements. However, we recommend that you use this or similar technology to provide a public code path for creating a control hierarchy.
The following code snippet defines a virtual data source.

Internal Sealed ClassDummydatasource: icollection {

Private IntDataitemcount;

Public Dummydatasource ( Int Dataitemcount ){
This . Dataitemcount = Dataitemcount;
}
// Implement other methods of the icollection interface.

Public Ienumerator getenumerator (){
Return   New Dummyperformanceenumerator (dataitemcount );
}

Private ClassDummydatasourceenumerator: ienumerator {

Private IntCount;
Private IntIndex;

Public Dummydatasourceenumerator ( Int Count ){
This . Count = Count;
This . Index =   - 1 ;
}

Public   Object Current {
Get {
Return   Null ;
}
}
// Define other methods of the ienumerator interface.
}
}

Dummydatasource can be used to define the createcontrolhierarchy method, as shown below.
Private   Void Createcontrolhierarchy ( Bool Usedatasource ){
Ienumerable datasource =   Null ;
Int Count =   - 1 ;

If (Usedatasource =   False ){
// Viewstate must have a non-null value for itemcount because this is checked
// By createchildcontrols.
Count = ( Int ) Viewstate [ " Itemcount " ];
If (Count ! =   - 1 ){
Datasource =   New Dummydatasource (count );
}
}
Else {
Datasource =   This . Datasource;
}

If (Datasource ! =   Null ){
Int Index =   0 ;
Count =   0 ;
Foreach ( Object Dataitem In Datasource ){

// Invoke a private helper method to create each item.
Createitem ();
Count ++ ;
Index ++ ;
}
}

If (Usedatasource ){
// Save the number of items contained for use in round trips.
Viewstate [ " Itemcount " ] = (Datasource ! =   Null ) ? Count: - 1 );
}
}

The createitem method is used to create a template and bind the dataitem attribute to the data source. The following code snippet describes how to implement the createitem method in the templated data binding control example. Note that the createitem method is implementation details and is not defined in. NET Framework.

Private Templatedlistitem createitem (Table table, Int Itemindex, listitemtype itemtype, Bool Databind, Object Dataitem ){
Templatedlistitem item =   New Templatedlistitem (itemindex, itemtype );
Templatedlistitemeventargs E =   New Templatedlistitemeventargs (item );

If (Itemtemplate ! =   Null ){
Itemtemplate. instantiatein (item. cells [ 0 ]);
}
If (Databind ){
Item. dataitem = Dataitem;
}
Onitemcreated (E );
Table. Rows. Add (item );

If(Databind ){
Item. databind ();
Onitemdatabound (E );

Item. dataitem= Null;
}

ReturnItem;
}
 

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.