Tuesday, June 26, 2007

Attributes to consider applying when writing a web custom control

Attributes to consider applying when writing a web custom control

Almost every custom control has at least one additional public property, and that public property as well as the control itself should probably have at least a few attributes applied to them.  Attributes tell the designer host (Visual Studio) or the parser (ASP.NET) interesting things about your control that might not be evident from just its name and its type.

Came across an interesting link that gives a quick overview of some of the useful and important attributes that you can apply to your control and its properties and events.

Following are some of the extracts from the same -

By applying the proper set of attributes you can significantly increase the usefulness of your control in several ways. For example, the DescriptionAttribute provides helpful text to the person designing the page. The ValidationPropertyAttribute is required when the person designing the page wants to validate the value of your control. Following is a list of the most useful and important attributes you can apply to your control and its properties and events.

Control attributes:

  • ControlValueProperty
     - Used by data source parameters to get the "intrinsic" value of the control. For example, DropDownList's "intrinsic" value is its SelectedValue property.
  • DefaultEvent
     - Set the event for which to create an event handler when double clicking the control in the designer.
  • DefaultProperty
     - Set the default selected property in the designer's property grid.
  • NonVisualControl
     - Hide the control at design time when "Non Visual Controls" is unchecked from the View menu.
  • ParseChildren
     - The full name is really "parse children as properties".
     - Set to true if the inner contents of the control represent properties as opposed to child controls.
     - True by default on controls deriving from WebControl; false by default otherwise.
  • PersistChildren
     - The full name is really "persist child controls".
     - Set to true if the designer should persist child controls as the inner contents.
     - False by default on controls deriving from WebControl; true by default otherwise.
     - 99.9% of the time PersistChildren has the opposite value of ParseChildren.
  • ValidationProperty
     - Required when a control can be validated.
     - Somewhat similar to the ControlValueProperty in that they often point at the same property.
  • ViewStateModeById
     - Indicates that viewstate should be loaded based on control IDs as opposed to being loaded based on the index of the control in the child controls collection.

Property attributes:

  • Bindable
     - Indicates at design time only whether the property should appear by default in the Edit Databindings dialog.
  • Browsable
     - Get-only property: Always false.
     - Get/Set property: Whatever you want.
  • Category
     - Determines in which category the property will appear when the property grid is in category mode.
  • DefaultValue
     - Get-only property: Since you have Browsable(false), and it's never persisted anyway, no default value is needed.
     - Get/Set value type property: Must be set.
     - Get/Set reference type property: Must be null so that it shows up as non-bold in the property grid.
  • Description
     - Determines the help text that will show in the property grid's lower help panel.
  • DesignerSerializationVisibility
     - Controls whether the property is persisted in the markup (see also PersistenceMode).
     - Use this to prevent get/set properties from being persisted at all.
  • PersistenceMode
     - Controls how the property is persisted in the markup.
     - Simple-valued properties should use the default, which is Attribute.
     - Collection, template, and complex (e.g. styles) should use InnerProperty.
     - InnerDefaultProperty should never be used since it causes compatibility problems. For example, if in the next version of your control you want another inner property, it won't work properly.
     - EncodedInnerDefaultProperty should also rarely be used for similar reasons as InnerDefaultProperty.
     - In ASP.NET 2.0 support was added for strings to be InnerProperties, which is good for large multi-line string values, such as XmlDataSource's Data property.

Event attributes:

  • Browsable
     - Same as properties.
  • Category
     - Same as properties.

Please note that the above is only a part of the complete list.
For details check out the link :



Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258


DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

No comments:

Post a Comment