How to change the default control analysis Logic
------ Use persistchildren (false) and controlbuilder to customize Asp.net's analysis of the content in the control label pair
"My website, I am the master"
Here, I replaced the music played by foobar with Jay Chou's song, although not "my site ".
Let's review the section in article 3.Code:
< ASP: dropdownlist ID = "Dropdownlist1" Runat = "Server" Font-bold = "True" >
< ASP: listitem Value = "1" > 1 </ ASP: listitem >
< ASP: listitem Value = "2" > 2 </ ASP: listitem >
</ ASP: dropdownlist >
I wonder if you have noticed that the content in <asp: listitem> cannot contain page objects. How can you deal with it if you have a analyzer ?. This reveals a piece of information for us. We can get started with the analysis and processing of page elements! (Oh, it's better than the dynamic zone .)
How can we intervene in the analysis of control elements?
1. parsechildrenattribute
2. controlbuilderattribute
Parsechildren is used to specify the analysis logic of the control. It has a bool parameter. True indicates that the content in the control label is interpreted as an attribute, the parser uses a set of internal control generators to parse nested attributes, subattributes, templates, and set attributes. You can also use parsechildren (true, "propertyname") to specify the attribute passed in by the nested content, which has been declared as true in webcontrol. If it is false, how can we analyze it? In this case, the parser uses the control-related controlbuilder to explain the start and end-label content of the Control. to interpret the content in the control as an object, the text is also interpreted as literalcontrols, add it to the control using the addparsedsubobject method of the control.
Well, let's look back at listitem. listitem is not inherited from webcontrol, and it is not declared as parsechildren (true). How does it implement its own analysis logic? The answer is that it uses controlbuilderattribute.
[Controlbuilder ( Typeof (Listitemcontrolbuilder),]
Public Sealed Class Listitem: istatemanager, iparseraccessor, iattributeaccessor
{
}
Let's take a look at the listitemcontrolbuilder class.
Public Class Listitemcontrolbuilder: controlbuilder
{
Public Listitemcontrolbuilder ()
{
}
Public Override Bool Allowwhitespaceliterals ()
{
Return False;//Removes the control label from the beginning and end of the nested content.
}
Public Override Bool Htmldecodeliterals ()
{
Return True;//Delete HTML Encoding
}
}
Here, we can disable HTML encoding in tags without using controlbuilder. We can reload the addparsedsubobject method of controls inherited from webcontrol.
Protected Override Void Addparsedsubobject ( Object OBJ) {
If (OBJ Is Literalcontrol) {
Text=(Literalcontrol) OBJ). text;
}
Else {
Throw NewArgumentexception (
"The inner content must contain static text");
}
// Do anything you want.
}
Note that this method is less efficient than the controlbuilder method, because it will be executed once every time the request implements the logic, and the former will only be executed once during the parsing period before the code is generated.
In addition, for controls inherited from webcontrol, you must use controlbuilder to control the generation of content objects. Remember to declare parsechilder (false ).