Complex attributes of ASP. NET 2.0 Server Control Development

Source: Internet
Author: User

In the previous article, I mentioned the concept of "complex attributes. 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 following source code.

 

Public class CustomerControl: WebControl {
[DesignerSerializationVisibility (DesignerSerializationVisibility. Content), policyparentproperty (true)]
Public SizeInfo Size {......}
}
As shown in the code above, 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. The value is set to the DesignerSerializationVisibility. Content enumeration value, which is used to specify the Content of the serialization attribute, that is, the sub-attribute, rather than the Size attribute itself, because the serialization Size has no significance. 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.

 

Related Article

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.