How to change the default control parsing logic
------Use PersistChildren (false) and controlbuilder to customize ASP.net's analysis of the contents of the control label pair
"My turf, my decision."
Write here, I put the music foobar play to Jay Chou's song, although not "my site."
Let's review a section of the code in the third article:
<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 do not know if you have noticed a phenomenon,<asp:listitem> content can not have page objects, so if there is a parser how to deal with it? This revealed a message to us, for the analysis of page elements, we are plugged in! (Oh, better than the dynamic zone.) )
So how do we intervene in the analysis of control elements?
1, ParseChildrenAttribute
2, ControlBuilderAttribute
ParseChildren is used to specify the parsing logic of the control, which has a bool parameter, true means that the content in the control label is interpreted as a property, and the parser resolves nested properties, child attributes, templates, and collection properties with a set of default control generator. You can also use ParseChildren (True, "PropertyName") to specify which property the nested content passes in, and the WebControl is declared true. So if it's false, how do you analyze it? In this case, the parser interprets the contents of the control start and the knot tag by the ControlBuilder associated with the control, and the text is interpreted as literalcontrols to interpret the contents as objects. The control's AddParsedSubObject method is then added to the control in controls.
Well, looking back, Listitem,listitem is not inherited from WebControl, and it is not declared as ParseChildren (true), so how does it implement its own analytic logic? The answer is that it uses ControlBuilderAttribute.
[ControlBuilder(typeof(ListItemControlBuilder)), ]
public sealed class ListItem : IStateManager, IParserAccessor, IAttributeAccessor
{
}
And look at the Listitemcontrolbuilder class.
public class ListItemControlBuilder : ControlBuilder
{
public ListItemControlBuilder()
{
}
public override bool AllowWhitespaceLiterals()
{
return false;//除去控件标签对嵌套内容首尾空白
}
public override bool HtmlDecodeLiterals()
{
return true;//删除HTML编码
}
}
Here, we can disable HTML encoding in the tag without controlbuilder, and we can overload a AddParsedSubObject method of a control inherited from WebControl
protected override void AddParsedSubObject(object obj) {
if (obj is LiteralControl) {
Text = ((LiteralControl)obj).Text;
}
else {
throw new ArgumentException(
"The inner content must contain static text ");
}
// do anything you want.
}
Note that this approach is less efficient than the ControlBuilder method because it executes once each time the request is implemented, and the former is executed only once in the parsing period before the code is generated.
In addition, you should remember to declare Parsechilder (false) to implement a ControlBuilder control to generate a Content object than a WebControl inherited control.