Parsechildren in the Custom Control

Source: Internet
Author: User

When I wrote a demo today, I encountered the following error:



Parser error message: Type 'servercontrol _ cachepanel. cachepanel 'does not have a public property named 'webusercontrol1 '.


Calm down and think for a moment. It may be related to parsechildren in the custom control.



If we embed a label control in a Textbox Control:

 

Code
< ASP: textbox ID = " Textbox1 " Runat = " Server " >
< ASP: Label runat = " Server " Text = " Label " > </ ASP: Label >
</ ASP: textbox >

 

The page displays an error message,ASP: The text attribute of textbox does not allow sub-objects, Cannot pass debugging, which obviously belongsAnalyzer ErrorHowever, you can write as follows:

Code
< ASP: dropdownlist ID = " Dropdownlist1 " Runat = " Server " >
< ASP: listitem > 1 </ ASP: listitem >
< ASP: listitem > 2 </ ASP: listitem >
< ASP: listitem > 3 </ ASP: listitem >
</ ASP: dropdownlist >

 

Why can some controls be embedded with other tags, while some controls cannot? Check msdn to find out the principle.

Msdn:


1: when developing an ASP. NET Server Control, the parsechildrenattribute class instructs the page analyzer how to handle the nested content in the server control tag declared on the page.


2: The parsechildrenattribute class allows you to tag server controls with parsechildrenattribute metadata to specify the analysis logic for custom server controls.


3: marking the server control with the metadata attribute parsechildren (true) will instruct the analyzer to interpret the elements included in the server control tag as properties ).

 

4: The Server Control marked with the metadata attribute parsechildren (true, "<default property>") sets the defaultproperty Property to pass to the property) property name.

 

5: marking the server control with the metadata property parsechildren (false) (default) will instruct the analyzer to interpret the elements contained in the server control tag as the content to be analyzed through the Associated controlbuilder, it is interpreted as a control.


Msdn gave a demo for the Application parsechildren (true) and parsechildren (false) respectively. For specific examples, I will not post them here. The conclusion is as follows:


1:When the [parsechildren (True] metadata attribute is applied, the generated attribute is eventually assigned to an attribute of collectionpropertycontrol.
2: When the [parsechildren (false] metadata attribute is applied, the generated control object is added to the controls set of collectionpropertycontrol.

 

Use of parsechildren (false): We can also use parsechildren (false) as usual)


1. Use the default page analysis Logic

 

Code
[Parsechildren ( False )]
Public   Class Contentclass: webcontrol
{

Protected   Override   Void Addparsedsubobject ( Object OBJ)
{
If(OBJIsContent)
Base. Addparsedsubobject (OBJ );
}

Protected   Override   Void Rendercontents (htmltextwriter writer)
{
//Load controls
}
}
// Child widget
Public   Class Content: Control
{
//Code omitted
}


PageCodeAs follows:

Code
< PC3: contentclass
Runat = " Server " >
< C0: Content
ID = " Content1 "
Runat = " Server " >
The first item displayed. This is not a property
</ C0: Content >
</ PC3: contentclass >

 

The control has the default page analysis logic. You can override the addparsedsubobject method to add a child control to the control, or do not duplicate the addparsedsubobject method. In this case, it is suitable for loading external controls and user controls.


 

Second, rewrite the default page analysis logic.


Each control has a default parsing logic, which is implemented through the controlbuilder class and can be rewritten to customize the parsing logic.

 

Code

// Custom Analyzer
Public   Class Customcontrolbuilder: controlbuilder
{
Public   Override Type getchildcontroltype ( String Tagname, idictionary attribs)
{
If (String. Compare (tagname, " Content " , True ) =   0 )
Return   Typeof (Customcontrol );
Else
Return   Null ;
}
}

// Define a simple control
Public   Class Customcontrol: Control
{
//Code omitted
}


Step 1: The customcontrolbuilder class overrides the getchildcontroltype method of the controlbuilder class to obtain the type of the control type corresponding to the sub-tag. In this method, the content mcontrol control is replaced by the content tag, and the page analysis logic is rewritten.


Step 2: Define a simple customcontrol control. You must also override the addparsedsubobject method in the parent control to add the customcontrol control to the subcontrol:

Code
Protected   Override   Void Addparsedsubobject ( Object OBJ)
{
If(OBJIsCustomcontrol)
Base. Addparsedsubobject (OBJ );
}

 

Step 3: Associate the control generator with the control, and set parsechildren (false)

Code
[Controlbuilder ( Typeof (Customcontrolbuilder)]
[Parsechildren ( False )]
Public   Class Webcoustomcontrol: webcontrol
{
}


Step 4: Page code:

Code
  < C0: webcoustomcontrol
Runat = " Server " >
< Content />
</ C0: webcoustomcontrol >

 

As for the use of parsechildren (true), we will not mention it this time. It is associated with the complex properties of the control.

 

Conclusion: In the Asp.net programming process, there are actually many very small points that we usually do not have a deep understanding of them because of insufficient attention, the problem may be hard to solve. Control development is very interesting and meaningful, so it is necessary to have a deeper understanding of it and its application. If there is anything wrong with this article, I hope you will criticize and advise me.

 

Note:

Reference:

1: msdn.

2: http://www.cnblogs.com/Clingingboy/archive/2006/09/29/514722.html

 

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.