Method for developing template controls

zhaozj2021-02-16  54

Method for developing template controls:

1. Implement the System.Web.ui.inamingContainer interface. (It can create a new naming range under the control, so that the subtree has a unique identifier in the name tree).

2. Apply PARSECHILDRENATTRIBUTE to the control and pass True as a parameter. Declare the control when the control is declare on the ASP.NET page, which can indicate how the page analyzer analyzes the template property tag. (That is, apply this metadata properties, You can make the page analyzer's correct analysis template's properties tag.

[Note: If the control is derived from WebControl, it is not used because WebControl has used this property as a tag.

3. Define the properties of one or more system.web.ui.Itemplate types. This property must have the system.web.ui.TemplateContainerattribute property, which pointing this itemplate type property will be owned by which INAMINGCONTAINER control. (That is, use the TemplateContainerattribute metadata attribute to identify the container of the template of the control)

4. Create a separate container control for the template control you are created. (Cause I am now not clear now, please see the contents of the template data binding control afterwards.

5. Rewote the CreateChildControls method and create a child control in the template.

A. Instantiate the template container.

B. Call the instantiatein method of the template property and pass the container as a parameter to it.

C. Add the example of the template container to the Controls collection of your template control.

Example: 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);}}

6. Overcrew the onDatabase for inherited from Control:

Protected Override Void OndataBinding (Eventargs E) {EnsureChildControls (); Base.OndDataBinding (E);

The EnSureChildControls method guarantees the child control in the template before the data binding.

Call the ONDATABINDING method of the base class to ensure that the registered event handler is called.

7. Perform the above steps to each template property in the control.

转载请注明原文地址:https://www.9cbs.com/read-21354.html

New Post(0)