Expando Attributes The IAttributeAccessor Interface

Interface"-->

Expando Attributes ”The IAttributeAccessor Interface

If the page developer declaratively specifies attributes on a Web control's tag that do not correspond to public properties of that control, the page parser adds those attributes as custom name /value pairs to the Attributes collection property of the control. This functionality is enabled because WebControl implements the System.Web.UI.IAttributeAccessor interface.

The Attributes property of WebControl allows the control's user to add custom HTML attributes that the control does not expose as properties. WebControl renders its Attributes collection in the form of HTML attributes without any processing on the server. Such user-added attributes are also referred to as expando attributes . Any control that derives from WebControl automatically provides support for expando attributes. This functionality is especially useful for tying in client-side script functionality, as shown in the following example, where the page has a TextBox control with two user-supplied attributes ( onmouseover and onmouseout ) that do not correspond to properties of the TextBox control:

 <%@PageLanguage="C#"%> <html> <head> <style> .beige{background-color:beige} </style> <scriptlanguage="JavaScript"> functionOnMouseOver(element){...} functionOnMouseOut(element){...} </script> </head> <body> <formrunat="server"> <asp:TextBoxrunat="server"  CssClass="beige" onmouseover="OnMouseOver(this)"onmouseout="OnMouseOut(this)  "/> </form> </body> </html> 

On the client, the markup rendered by the TextBox instance is similar to the following HTML:

 <inputname="_ctl0"type="text"class="beige" onmouseover="OnMouseOver(this)"onmouseout="OnMouseOut(this)"/> 

If a user tries to add expando attributes to a control that does not implement the IAttributeAccessor interface, the parser will throw an exception. The Control class does not implement the IAttributeAccessor interface because expando attributes are not meaningful in all server controls. If you want to support expando attributes in a custom control that derives from Control , you must implement the IAttributeAccessor and define an Attributes property. The following code shows how WebControl implements these members :

 publicclassWebControl:Control,IAttributeAccessor{ privateStateBag_attrState; privateAttributeCollection_attrColl; [ Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) ] publicAttributeCollectionAttributes{ get{ if(_attrColl==null){ if(_attrState==null){ _attrState=newStateBag(true); if(IsTrackingViewState) _attrState.TrackViewState(); } _attrColl=newAttributeCollection(attrState); } return_attrColl; } } [ Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) ] publicboolHasAttributes{ get{ return(_attrState!=null)&&(_attrState.Count>0); } } stringIAttributeAccessor.GetAttribute(stringname){ return((HasAttributes!=null)? (string)_attrState[name]:null); } voidIAttributeAccessor.SetAttribute(stringname,stringvalue){ Attributes[name]=value; } } 

The AttributeCollection type of the Attributes property has its own StateBag and manages its own state by implementing IStateManager . To support expando attributes in a custom control that derives from Control , you can use the preceding implementation as a starting point. You can then perform custom state management in your control by using the pattern we showed earlier in the chapter for the ImageMap control, which has a collection property that manages its own state.



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