Template control concept)

Source: Internet
Author: User
Template control concept:

The template control separates content from appearance. Typical applications include repeater and datalist.

ASP. NET provides a common feature called a template that allows you to separate control data from its representation. The templated control itself does not provide a user interface (UI ). The UI of the control is provided by PAGE developers through an inline template, which allows page developers to customize the UI of the control.

 

Method for developing templated controls:

Implement the system. Web. UI. inamingcontainer interface. This is a tag interface without any methods. It can create a new name range under your control, so that the control has a unique identifier in the name tree.
[C #]
Public   Class Templatedfirstcontrol: control, inamingcontainer {}
Apply parsechildrenattribute to your control and pass True As a parameter. When the control is declared on the ASP. NET page, this instructs the page analyzer how to analyze the template property tag. Procedure 3 Describes how to define a template attribute.
NOTE: If your control is derived from webcontrol, you do not need to apply parsechildrenattribute because webcontrol has already marked this attribute.
[C #]
[Parsechildren (childrenasproperties =   True )]
Public   Class Templatedfirstcontrol: control, inamingcontainer {}
For more information about parsechildrenattribute, see use parsechildrenattribute.
Define one or more attributes of the system. Web. UI. itemplate type. Itemplate has a method instantiatein which you can use the template provided on the page to create controls. You do not need to implement the instantiatein method. This implementation can be provided by the ASP. NET page framework. The itemplate attribute must have a metadata attribute of the system. Web. UI. templatecontainerattribute type. It specifies which inamingcontainer control will have an instantiation template. This is in step 4 . BelowCodeThe snippet defines a template attribute.
[C #]
[Templatecontainer ( Typeof (Firsttemplatecontainer)] Public Itemplate firsttemplate {}
The templatecontainerattribute parameter is the container control type in which you want to instantiate the template. The container control is independent of the templated control being created. The reason for the logic container is that the templated control usually has a template that needs to be instantiated repeatedly using different data. It has different container controls than the root template controls, making it possible to have multiple such examples. The logical container is the real-time inamingcontainer of the subcontrol in the template. This relationship is described in more detail in the Development of templated data binding controls.
Note that the container control must implement inamingcontainer because it has a child control that must be uniquely named on the page.
[C #]
Public   Class Firsttemplatecontainer: control, inamingcontainer {}
Override the createchildcontrols method to create a child control in the template. This is done in three steps.
Instantiate the template container.
Call the instantiatein method of the template attribute and pass the container as a parameter to it. The instantiatein method (declared in the itemplate Interface) instantiate the elements of the template as sub-controls of the template container. You do not need to implement the instantiatein method. This implementation can be provided by the ASP. NET page framework.
Add the template container example to the controls set of your templated control.
The following code snippet illustrates the implementation of createchildcontrols.
[C #]
Private Control mytemplatecontainer;
Protected   Override   Void Createchildcontrols (){
If (Firsttemplate ! =   Null )
{
Mytemplatecontainer =   New Firsttemplatecontainer ( This );
Firsttemplate. instantiatein (mytemplatecontainer );
Controls. Add (mytemplatecontainer );
}
Else
{
Controls. Add ( New Literalcontrol (Text +   "   "   + Datetime ));
}
}
Override the ondatabinding method inherited from control to call the ensurechildcontrols method. This ensures that the child control in the template is created before the page framework tries to calculate any data binding expression in the template. You must also call the ondatabinding method of the base class to ensure that the registered event processing is called.Program.
[C #]
Protected   Override   Void Ondatabinding (eventargs e ){
Ensurechildcontrols ();
Base . Ondatabinding (E );
}
In step 5 In the createchildcontrols method, repeat the logic to instantiate a template for each template attribute of the control.
 

Related Knowledge:

 

Concepts about parsechildrenattribute:

Parsechildrenattribute is the. NET Framework metadata attribute applied at the class level. It enables the control to specify how the page analyzer interprets nested elements in the control tag when the control is declared on the ASP. NET page. You can create parsechildrenattribute by using the name parameter corresponding to the Boolean flag and the default attribute of the control. For more information about the property syntax, see system. Web. UI. parsechildrenattribute.

 

The following example shows how to apply parsechildrenattribute to a control.

 

[C #]

[Parsechildren (childrenasproperties= True)]

//Or apply as [parsechildren (true)].

Public ClassTemplatedfirstcontrol: control, inamingcontainer {}

 

[Visual Basic]

<Parsechildren (childrenasproperties:=True)>_

Public class templatedfirstcontrol

Inherits Control

Implements inamingcontainer

End Class

 

For examples of controls marked with parsechildrenattribute, see templated controls. When templatedfirstcontrol is declared on the page, the child elements (direct child elements) in the control tag must correspond to the attributes of templatedfirstcontrol, as shown in the following example.

 

<% --FirsttemplateIsA Property of templatedfirstcontrol.-- %>

<% -- Custom is the tag prefix for templatedfirstcontrol on the page as defined in the <% @ register %> directive. -- %>

Custom: templatedfirstcontrol id = " first " text = " the time on the server is " runat = server >

<Firsttemplate>

< H3 > < Font face = " Verdana " Color =   " Red " > <% # Container. Text %>   <% # Container. datetime %>

</Font> </H3>

</Firsttemplate>

</Custom: templatedfirstcontrol>

 

The following table describes the usage modes of parsechildrenattribute.

 

Attribute usage instructions

Parsechildrenattribute (childrenasproperties= True)

Or

 

Parsechildren (True)

The nested (child) element must correspond to the properties of the control. Other (non-attribute) elements and text generation analyzer errors between control tags.

Example: repeater and other data binding controls.

 

Parsechildrenattribute (childrenasproperties= False)

Or

 

Parsechildrenattribute (False)

 

Or

 

Parsechildrenattribute should not be used for this control.

Nested (child) elements must correspond to ASP. NET Server controls. Page analyzer creates a child control and calls iparseraccessor. addparsedsubobject for the control. By default, addparsedsubobject adds the child control to the controls set of the control. Page analyzer analyzes text between tags as literalcontrol instances.

Example: panel.

 

Parsechildrenattribute (childrenasproperties= True, Defaultproperty= "Propertyname")

Or

 

Parsechildrenattribute (True,"Propertyname")

The control must define the public property name propertyname. The nested (child) element must correspond to the child element of the propertyname attribute.

Example: htmltable and htmltablerow.

 

For an example, see parsechildrenattribute.

 

Parsechildrenattribute (False,"Propertyname") This usage of page analyzer is equivalent to parsechildrenattribute (False).

 

Note that system. Web. UI. webcontrols. webcontrol is marked as parsechildren (True). Custom Controls derived from webcontrol inherit this property tag. You can re-apply attributes with different parameters to override the inherited attributes.

Note that if controlbuilderattribute is applied in addition to parsechildrenattribute, it can modify the analysis logic described in this table.

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.