In the previous articleArticleThe concept of "complex attributes" has been mentioned. The biggest feature of a complex attribute is that the attribute type is a class with an attribute (called a subattribute. Generally, complex attributes are represented in three forms: character-connected attributes, internal nested attributes, and internal nested default attributes. This article describes how to implement complex attributes in the preceding three forms.
1. implement complex properties in the form of hyphens
The concatenation form attribute is a common complex attribute. The common font attribute is a complex attribute, which includes multiple sub-attributes, such as bold and name. This type of property has two syntax formats: one is to save the sub-property in the start tag of the control using the concatenation syntax, such as font-bold and font-name. Another format is to save sub-attributes in the tag of the control, for example, <font bold = "true"/>. The latter is more readable than the former.
To implement a complex attribute in the form of a hyphen, you must set the specified metadata for the complex attribute and its sub-attributes. The following describes how to set the metadata of a complex attribute. Please read the followingSource code.
Public class customercontrol: webcontrol {
[Designerserializationvisibility (designerserializationvisibility. content), policyparentproperty (true)]
Public sizeinfo size {......}
}
AboveCodeAs shown in, size is a complex attribute, and its attribute type is sizeinfo (custom class ). Two metadata settings are set before the size attribute implementation: designerserializationvisibility and policyparentproperty. Designerserializationvisibility is used to specify the persistence type used to serialize attributes on the component at design time. Its value is set to the designerserializationvisibility. Content enumeration value, used to specify the serializationProgramThe content of the serialized attribute is a sub-attribute, not the size attribute itself, because the serialization size does not make any sense. In addition, it includes a policyparentproperty (true) setting, which enables the property browser to upload the modification notifications for the sub-attributes to the object model, and a notification of modification is generated in the control with the sub-Attribute Modified.
After the metadata settings of complex attributes are completed, developers must also set metadata related to the child attributes. For example, size contains two sub-attributes: height and width. The implementation code of these sub-attributes is as follows.
[Typeconverter (typeof (expandableobjectconverter)] public class sizeinfo {
[Policyparentproperty (true)]
Public uint32 height {......}
[Policyparentproperty (true)]
Public uint32 width {......}
}
As shown in the code above, the sub-attribute height and width are respectively set with metadata policyparentproperty (true ). In this way, when the sub-attribute is modified, the. NET Framework will automatically generate a change notification and notify the parent attribute size. In addition, there is a design-time special typeconverter (typeof (expandableobjectconverter) that tells the property browser to Provide extensions and folding styles so that control developers can directly edit sub-attributes in the property browser.
The preceding describes how to declare the form attribute of a hyphen. We can see that two key points need to be taken into account in the process of declaring a hyphen attribute: one is the metadata settings for designing complex attributes, and the other is the feature settings for designing subattributes.
2. implement complex attributes in the internal nesting form
Generally, developers implement complex properties in the form of hyphens. However, complex attributes can also be nested internally. The following code is a typical internal escape form complex attribute application.
<Mycontrol: customecontrol id = "demo1" runat = "server">
<Headerstyle forecolor = "# FFFF00" backcolor = "#99ff00">
</Headerstyle>
......
</Mycontrol: customecontrol>
As shown in the code above, the property headerstyle of the custom control mycontrol is a typical internal nested form attribute. The implementation of this form of attribute is very different from the implementation of the form of a hyphen attribute, need to be divided into two situations.
If the custom server control class inherits the control class, you must set the metadata attributes parsechildren and persistchildren before the control class. The schematic code is as follows.
[Parsechildren (true), persistchildren (false)]
Public class customecontrol: Control {......}
As shown in the code above, two metadata attributes parsechildren and persistchildren are set before the control class. The former is used to notify the page analyzer to parse the content marked by the control into a property or a child control. If the property value is set to true, it indicates that the property is parsed. The latter is used to notify the designer to save the content marked in the control as a property or a sub-control. The property value is set to false, indicating that the content is saved as a property.
If the custom control class inherits from the webcontrol class, the preceding metadata attribute settings are not required, because the webcontrol class has applied these metadata attributes.
No matter whether the custom control class inherits from the webcontrol class or the control class, the following metadata attributes must be set in the attribute implementation to implement complex attributes in the internal nesting form.
[Designerserializationvisibility (designerserializationvisibility. content), yyparentproperty (true), persistencemode (persistencemode. innerproperty)]
Public tableitemstyle headerstyle {......}
The code above shows that three metadata attributes must be applied before complex attributes are implemented: designerserializationvisibility, policyparentproperty, and persistencemode. The first two metadata attributes are described earlier. The third persistencemode is used to specify how to maintain the server control attributes or events to ASP. the metadata attribute of the. NET page. The value of this attribute is set to the enumerated value: persistencemode. innerproperty, which indicates that the identified property (headerstyle) is kept as a nested tag.
The preceding describes how to declare internal nested attributes. To sum up, there are two cases: one is that the developed control is derived from control, you need to set five design-time features: parsechildrenattribute (true), persistchildren (false), designerserializationvisibility, policyparentproperty, and persistencemode. The first two features are set before the control class to tell the compiler to set the content in the control tag as an attribute and need to be resolved as an attribute. The last three features are specified before the property, it is used to indicate the compiler that this attribute is an internal nested attribute. When applying the control attribute, it must be nested. Second, the developed control is derived from webcontrol, which is relatively simple. You only need to set the last three design-time features in the above article.
3. Implement the default complex attributes of the internal nesting form
The default attribute of the internal nesting form is very similar to that of the internal nesting form. It is usually used to set the set attribute of a control. For example, the attributes in the datalist and dropdownlist controls of the Standard Server Control are default internal nested attributes.
To implement this type of attribute, you must set two metadata attributes: first, set parsechildren (true, "defaultpropertyname") before the control class, and specify the nested tag attribute in the control, instead of a child control, the nested attributes are analyzed as the set attributes of the control. The second is to set the persistencemode (persistencemode) before the set attributes. innerdefaultproperty), which defines this property as the default property of the control.
4. Summary
This section describes how to create complex attributes. This is the focus and difficulty in implementing custom server controls. In the subsequent article, we will use examples to deepen our understanding of the implementation methods of complex attributes.