ASP.net 2.0 server control development complex properties

Source: Internet
Author: User
Tags implement key object model
Asp.net| Server | control | control DEVELOPMENT

InPrevious Article, the concept of "complex attributes" has been mentioned. The maximum characteristic of a complex property is that the type of the property is a class that itself has attributes, called child attributes. In general, complex properties are represented in 3 forms: hyphen-form properties, internal nested-form properties, and internal nested-form default properties. This article will introduce the concrete implementation of the above 3 kinds of complex attributes.

   1. Implement a hyphen form complex property

The hyphen form attribute is a more common complex property. Our common font property is a complex property that includes several child attributes, such as bold, name, and so on. This type of attribute has two syntactic formats: one is to use the hyphen syntax to save child properties in the control's opening tag, for example, Font-bold,font-name. Another format is to save child properties in the control's markup, for example, <font bold= "true"/"." The latter is more readable than the former.

To implement a complex property of a hyphen, you must set the specified design-time metadata for the complex property and its child property implementations. The following is an example of the 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 {...}
}
As shown in the code above, size is a complex property with a property type of Sizeinfo (custom Class). Two design-time metadata was set before the Size property was implemented: DesignerSerializationVisibility and Notifyparentproperty. DesignerSerializationVisibility is used to specify the type of persistence used when serializing a property 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 attribute, that is, the child property, not the Size property itself, because serialization size has no meaning. Also included is a notifyparentproperty (true) setting that enables notification of modification of a child property in the property browser to be uploaded to the object model and produces a change notification in the control that is modified.

After the metadata settings for complex properties have been completed, the developer must also set the related design-time metadata for the child property. For example, the size includes two child property height and width, and their implementation code should look like this.

[TypeConverter (typeof (Expandableobjectconverter))]public class sizeinfo{
[Notifyparentproperty (True)]
Public UInt32 Height {...}
[Notifyparentproperty (True)]
Public UInt32 Width {...}
}
As shown in the code above, the child property height and width are set to the metadata Notifyparentproperty (true) respectively. This way, when a child property changes. NET Framework automatically generates a change notification and notifies the parent property size. In addition, there is a design-time feature TypeConverter (Expandableobjectconverter) that tells the property browser to provide extended and collapsed styles so that the control developer can edit the child properties directly in the property browser.

The above describes a method for declaring a hyphen-form attribute. From this, we can see that there are two key points to be grasped in the process of declaring the hyphen attribute: One is the design-time metadata setting of the complex attribute and the other is the design-time attribute setting of the child attribute.

   2. Implement internal nested form complex properties

Typically, developers implement more complex properties in a hyphen form. However, you can also implement an internal nested form for complex properties. The following code is a typical application of complex attributes of the internal abscond form.

<mycontrol:customecontrol id= "Demo1" runat= "Server"
</HeaderStyle>
... ...
</MyControl:CustomeControl>
As shown in the code above, the property HeaderStyle of a custom control mycontrol is a typical internal nested form property. The implementation of this form of property and the implementation of the hyphen form of the property is very different, need to be divided into two situations.

If your custom server control class inherits from the control class, you must set the metadata properties ParseChildren and PersistChildren before the controls 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 before the control class. The former is used to tell the page parser whether the content in the control's markup resolves to a property or a child control, and the property value is set to true to indicate resolution to the property. The latter is used to tell the designer to save the contents of the control tag as a property or a child control, set to false, to indicate that it is saved as a property.

If the custom control class inherits from the WebControl class, then the above metadata property settings are not required because the WebControl class has already applied the metadata properties.

The following metadata properties must be set in the property implementation, regardless of whether the custom control class inherits from the WebControl class or the controls class in order to implement an internal nested form complex property.

[DesignerSerializationVisibility (Designerserializationvisibility.content), Notifyparentproperty (true), PersistenceMode (Persistencemode.innerproperty)]
Public TableItemStyle headerstyle{...}
As you can see from the above code, you must apply 3 metadata attributes before implementing complex properties: DesignerSerializationVisibility, Notifyparentproperty, and PersistenceMode. The first two metadata attributes are described in the previous article. The third persistencemode is used to specify how the server control property or event is persisted to the metadata property of the ASP.net page, and the value of the attribute is set to the enumeration value: Persistencemode.innerproperty, which means that the identified property (HeaderStyle) remains embedded Set of tags.

The above describes the methods for internal nested form attribute declarations. summed up in two cases: one is that the developed control derives from controls, you need to set five design-time attributes ParseChildrenAttribute (true), PersistChildren (false), DesignerSerializationVisibility, Notifyparentproperty and PersistenceMode. The first two attributes are set before the control class. Used to tell the compiler that the contents of the control tag are properties and need to be resolved to a property; the last three attributes are specified before the property to instruct the compiler that this property is an internal nested form property and must be nested when the control property is applied. The second is that the developed control derives from the WebControl, which is simpler, simply by setting the 3 design-time features above.

   3. Implement internal nested form default complex properties

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

In order to implement this form of property, the primary need is to set two metadata properties: First, set ParseChildren (True, "Defaultpropertyname") before the control class, and specify that the nested markup in the control represents a property, not a child control, The nested property is parsed as a collection property of the control, and the second is to set the attribute PersistenceMode (Persistencemode.innerdefaultproperty) before the collection property to indicate that the property is defined as the default property of the control.

   4. Summary

This article describes the implementation method for creating complex attributes. This is the key and difficult content of implementing a custom server control process. In a subsequent article, we will use examples to deepen our understanding of the methods of implementing complex properties.

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.