Asp.net Control Tag The content between them is sometimes processed as nodes and sometimes as child controls. For example, an Inter-node is processed as the property text value, and a control such as panel is processed as a sub-control. First, let's take a look at how to process attributes.
1. process as attribute
For the content between control labels, we can use the class system. Web. UI. parsechildrenattribute. Control Control's parsing behavior on its content.
Parsechildrenattribute is a Class-Level Attribute identifier, which has four constructors:
1. parsechildrenattribute (): If the childrenasproperties attribute is set to false, it cannot be parsed into a control.
2. parsechildrenattribute (Boolean), which is resolved to the control property.
3. parsechildrenattribute (type), in the control label Content It is parsed into a control of the type.
4. parsechildrenattribute (Boolean, string) specifies the value of childrenasproperties and the default parsed property (defaultproperty ).
By default, control parses the content into a child control, while webcontrol can identify [parsechildren (true, "text")], so it is parsed as an attribute by default. for simple properties such as Textbox, you only need to represent it as [parsechildren (true, "text"), defaultproperty ("text")]. For the items attribute of the dropdownlist control, if [parsechildren (true, "item"), defaultproperty ("items")] is identified, how does one parse items internally? Therefore, you must tell the resolution method corresponding to the property.
System. Web. UI. persistencemodeattribute is used to control the storage mode of control attributes. The received parameters are the persistencemode enumerated values. There are four types:
◆ Attribute: Specify attribute or event persistence as attribute );
◆ Encodedinnerdefaultproperty: Specifies the attribute to be kept as the unique internal text of the ASP. NET Server Control. The property value is HTML encoded. This can only be specified for strings;
◆ Innerdefaproperty property: Specifies the attribute to be kept as internal text in the ASP. NET Server Control. Also indicates defining this property Element . Only one attribute can be specified as the default attribute;
◆ Innerproperty: Specifies the attribute to be nested in the ASP. NET Server Control. This is usually used for complex objects that have their own persistence attributes.
Therefore, we can see that the identifier of the dropdownlist items attribute is [persistencemode (persistencemode. innerproperty)], and then it is identified as [persistencemode [persistencemode. encodedinnerdefaproperty property] on the text attribute of listitem Because the items attribute is a complex attribute and tags are nested, we can be certain that the identifier type is innerproperty, and text is the items attribute, the content in the default text is text, so it is identified as encodedinnerdefaproperty property.
2. processing as a child Control
The above describes some general attributes for processing as attributes. Method And sometimes we need to process the nodes in the tag as child controls, such as control controls. Generally, we can resolve the content in the tag into a sub-control using two methods: 1. Use the control. addparsesubobjec () method, 2. Build a new controlbuilder category.
Addparsesubobject () has been Analysis It is called only after it is released. It notifies the server control that a certain element (XML or HTML) has been analyzed in syntax and adds the element to the controlcollection object of the server control.
However, if we want to perform some analysis without loading, it is impossible to rely only on addparsesubobjec (). For example, if we want to remove some illegal strings before the analysis, it will not work. Therefore, we can create our own controlbuilder class for implementation. We only need our own controlbuilder class to inherit from the controlbuilder class for accurate parsing.
Example:
// Custom ControlBuilder class. Interprets nested tag name "myitem" as a textbox.
public class MyControlBuilder : ControlBuilder
{
public override Type GetChildControlType(String tagName,
IDictionary attributes)
{
if (String.Compare(tagName, "myitem", true) == 0)
{
return typeof(TextBox);
}
return null;
}
}
[
ControlBuilderAttribute(typeof(MyControlBuilder))
]
public class MyControl : Control
{
// Store all the controls specified as nested tags.
private ArrayList items = new ArrayList();
// This function is internally invoked by IParserAccessor.AddParsedSubObject(Object).
protected override void AddParsedSubObject(Object obj)
{
if (obj is TextBox)
{
items.Add(obj);
}
}
// Override 'CreateChildControls'.
protected override void CreateChildControls()
{
System.Collections.IEnumerator myEnumerator = items.GetEnumerator();
while(myEnumerator.MoveNext())
this.Controls.Add((TextBox)myEnumerator.Current);
}
}