Complex properties for ASP.NET 2.0 server control development

  • 2020-05-05 11:08:08
  • OfStack

In the previous article, the concept of "complex properties" was mentioned. The biggest feature of a complex property is that the type of the property is a class that has the property itself (called a child property). Typically, complex properties take three forms: a hyphen form property, an internally nested form property, and an internally nested form default property. This article will introduce the concrete implementation methods of the above three forms of complex attributes.

1. Implement the complex attribute

in hyphen form

Hyphenated properties are common and complex properties. The commonly used Font attribute is a complex attribute, which includes many sub-attributes, such as Bold, Name, and so on. There are two syntax formats for this type of property: one USES the hyphen syntax to save child properties in the start tag of the control, for example, Font-Bold, Font-Name. Another format is to save child properties in the tag of the control, for example, < font Bold="true" / >. The latter is more readable than the former.

To implement a complex property in hyphen form, you must set the specified design-time metadata for that complex property and its child property implementations. The following begins with an example of related metadata Settings in the implementation of complex properties. Please read the source code below.

 

public class CustomerControl:WebControl{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)]
public SizeInfo Size & # 123; . The & # 125;
}
As shown in the code above, Size is a complex property of type SizeInfo (custom class). Two design-time metadata are set before the Size property is implemented: DesignerSerializationVisibility and NotifyParentProperty. DesignerSerializationVisibility is used to specify the persistence type to use when serializing properties on a component at design time. The value is set to the DesignerSerializationVisibility.Content enumeration value, which specifies that the serializer should serialize the contents of the property, the child property, rather than the Size property itself, because serializing Size makes no sense. In addition, there is an NotifyParentProperty(true) setting that enables notification of changes to child properties in the property browser to be uploaded all the way to the object model and to generate notification of changes in the control where the child property has been modified.

After completing the metadata setting for the complex properties, the developer must also set the relevant design-time metadata for the child properties. For example, Size contains two child attributes Height and Width, and their implementation code should look like this.

[TypeConverter(typeof(ExpandableObjectConverter))]public class SizeInfo{

[NotifyParentProperty (true)] public UInt32 Height & # 123; . The & # 125;

[NotifyParentProperty (true)] public UInt32 Width & # 123; . The & # 125;
}
As shown in the code above, the child properties Height and Width are set to the metadata NotifyParentProperty(true), respectively. In this way, when a child property is modified, the.NET framework automatically generates a change notification to the parent property Size. In addition, there is a design-time feature called TypeConverter(typeof(ExpandableObjectConverter)) that tells the property browser to provide extension and collapse styles so that control developers can edit child properties directly in the property browser.

The methods for declaring hyphenated properties were described above. It can be seen that there are two important points to grasp in the process of declaring hyphen attributes: one is the design time metadata setting of complex attributes; The second is the design-time property setting of the child property.

2. Implement the complex property

with internal nested forms

Typically, developers implement complex properties in hyphen form. However, you can also implement nested forms internally for complex properties. The following code is a typical application of complex properties in the form of internal absconding.

 

< 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 internally nested property. Properties that implement this form are quite different from those that implement hyphen form properties and need to be divided into two cases.

If the custom server control class inherits from the Control class, you must set the metadata properties ParseChildren and PersistChildren before the control class. The schematic code is shown below.

[ParseChildren(true),PersistChildren(false)]
public class CustomeControl:Control{ ......}
As shown in the code above, two metadata properties ParseChildren and PersistChildren are set in front of the control class. The former is used to tell the page analyzer whether to parse the content in the control tag into a property or a child control, and the property value is set to true, which means it is resolved as a property. The latter tells the designer whether to save the content in the control tag as a property or as a child control, with the property value set to false, which means to save as a property.

If the custom control class inherits from the WebControl class, the above metadata property Settings are not required because the WebControl class already applies the metadata properties.

Whether the custom control class inherits from the WebControl class or the Control class, the following metadata properties must be set in the property implementation to implement internally nested complex properties.

[ DesignerSerializationVisibility( DesignerSerializationVisibility.Content), NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty)]
public TableItemStyle HeaderStyle{......}
As you can see from the code above, three metadata properties must be applied before complex properties can be implemented: DesignerSerializationVisibility, NotifyParentProperty, and PersistenceMode. The first two metadata properties were explained earlier, and the third, PersistenceMode, is used to specify how a server control property or event is maintained to the metadata property on the ASP.NET page. The value of this property is set to the enumeration value: PersistenceMode.InnerProperty, which means that the identified property (HeaderStyle) is maintained as a nested tag.

The above describes the method of internally nesting formal attribute declarations. This is summed up in two situations: one is that the developed control is derived from Control, and you need to set five design-time features ParseChildrenAttribute(true), PersistChildren(false), DesignerSerializationVisibility, NotifyParentProperty, and PersistenceMode. The first two properties are set in front of the control class to tell the compiler to interpret the contents of the control tag as properties. The last three attributes are specified before a property to indicate to the compiler that this property is an internally nested form property, which must be used when applying control properties. The second is that the developed control is derived from WebControl, which is a simple case of setting the last three design-time features above.

3. Implement the default complex property

in the form of internal nesting

The internal nested form default property is very similar to the internal nested form property, which is typically used to set the collection property of a control. For example, the properties in the DataList and DropDownList controls in the standard server control are default properties in the form of internal nesting.

To achieve this form of property, there are mainly two metadata properties to set: one is to set ParseChildren(true, "DefaultPropertyName") in front of the control class, specify that the nested tags in the control represent the property, not the child control, and analyze the nested property as the collection property of the control; The second is to set the property PersistenceMode(PersistenceMode.InnerDefaultProperty) before the collection property to indicate that this property is defined as the default property of the control.

4. Summary

This article describes how to create complex properties. This is the focus and difficulty of implementing custom server controls. In a subsequent article, we'll use examples to deepen our understanding of how complex properties are implemented.

 


Related articles: