Design-Time Attributes for Properties


In the section "Metadata Attributes" in Chapter 3, we provided an overview of metadata attributes in the .NET Framework. In the PageTracker example in the next section of this chapter, you will see several metadata attributes in action. We'll apply design-time attributes to properties of the PageTracker control to inform the designer how to display and serialize properties at design time.

You should apply a core set of design-time attributes to properties to enhance the design-time behavior of your control. Table 7-5 lists design-time metadata attributes that are commonly applied to properties. A complete list of design-time attributes is provided in Appendix A, "Metadata Attributes." Note that design-time attributes are applied only to public properties because properties that are not public are never visible in the designer.

Table 7-5. Design-Time Metadata Attributes for Properties  

Attribute

Description

BindableAttribute

Specifies whether it is meaningful to bind data to the property. We'll discuss data binding in Chapter 16, "Data-Bound Controls."

BrowsableAttribute

Specifies whether the property should be displayed in the property browser. By default, a public property is always displayed in the property browser. Apply this attribute as Browsable(false) only if you do not want a property to be displayed. You should generally mark read-only properties as Browsable(false) .

CategoryAttribute

Specifies the category that a property should be grouped by in the property browser. When the page developer selects the category filter, this attribute makes it possible to organize properties in logical groupings, such as Appearance, Behavior, Data, and Layout.

DefaultValueAttribute

Specifies a default value for the property, which is used by the designer to assign a value to the property. The value that you pass into this attribute should be the same as the default value returned by the property.

DescriptionAttribute

Provides a brief description of the property that is displayed at the bottom of the property browser when the page developer selects the property.

DesignerSerialization ­VisibilityAttribute

Specifies whether and how a property should be serialized in code. For example, you should mark a read-only property as DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) so that the property is excluded from the designer's serialization mechanism. You will see an example of this attribute in the PageTracker example in the next section. We'll discuss this attribute in more detail in Chapter 10.

Multiple Meanings of Attribute

In this book, we use the term attribute to mean three different things:

  • Metadata attributes that are used in managed code, such as Browsable(false)

  • Attributes within a control's tag that are used for setting a control's properties in ASP.NET declarative page syntax ”for example, <asp:TextBox Text="Some Text" runat ="server"/>

  • HTML attributes in an HTML element that is rendered to the ­client

In general, the meaning of the term attribute is clear from the context. But where there is ambiguity, we'll qualify attribute as a .NET metadata attribute, an attribute in a control's tag, or an HTML attribute rendered by a control.

Overriding an Attribute

You can override the attributes that your control inherits from its base class. To modify existing attributes on a property, you have to override the property and reapply attributes to it. For example, the BackColor property that a control inherits from WebControl is displayed in the property browser by default. If you do not want this property to be displayed in the property browser, you can override the BrowsableAttribute as shown in the following example:

 [Browsable(false)] publicoverrideColorBackColor{ get{ returnbase.BackColor; } set{ base.BackColor=value; } } 


Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net