IAttributeAccessor Interface


Defines methods used by ASP.NET server controls to provide programmatic access to any attribute declared in the opening tag of a server control.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Public Interface IAttributeAccessor


When implemented by a class, retrieves the specified attribute property from the server control.

System_CAPS_pubmethodSetAttribute(String, String)

When implemented by a class, designates an attribute and its value to assign to the ASP.NET server control.

If you author a custom server control that inherits from the WebControl, HtmlControl, or ListItem class, the .NET Framework automatically provides programmatic access to attributes because each of these classes implement the IAttributeAccessor interface.

If you author a custom server control that does not inherit from one of these classes and plan to allow programmatic access to attributes that do not correspond with the control's strongly typed properties, be sure to implement the IAttributeAccessor interface.

' The following class creates a custom ASP.NET server control that implements
' the IAttributeAccessor interface. It creates a MyTextBox class that contains
' Width and Text properties that get and set their values from view state.
' Pages that use this control create an instance of this control and set the
' Width property using the IAttributeAccessor.SetAttribute method. 
' The page then displays the values of the Text and Width properties 
' using the IAttributeAccessor.GetAttribute method.
' When compiled, this assembly is named MyAttributeAccessor.
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Security.Permissions

Namespace AttributeAccessor

 <AspNetHostingPermission(SecurityAction.Demand, _
   Level:=AspNetHostingPermissionLevel.Minimal)> _
 Public NotInheritable Class MyTextBox
   Inherits Control
   Implements IAttributeAccessor 

   ' Declare the Width property.   
   Public Property Width() As String
         Return CType(ViewState("Width"), String)
      End Get
         ViewState("Width") = value
      End Set
   End Property

   ' Declare the Text property.

   Public Property Text() As String
         Return CType(ViewState("Text"), String)
      End Get
         ViewState("Text") = value
      End Set
   End Property

   ' Implement the SetAttribute method for the control. When
   ' this method is called from a page, the control's properties
   ' are set to values defined in the page.
   Public Sub SetAttribute(name As String, value1 As String) Implements IAttributeAccessor.SetAttribute
      ViewState(name) = value1
   End Sub 'SetAttribute

   ' Implement the GetAttribute method for the control. When
   ' this method is called from a page, the values for the control's
   ' properties can be displayed in the page.
   Public Function GetAttribute(name As String) As String Implements IAttributeAccessor.GetAttribute
      Return CType(ViewState(name), String)
   End Function 'GetAttribute

   Protected Overrides Sub Render(output As HtmlTextWriter)
      output.Write(("<input type=text id= " + Me.UniqueID))
      output.Write((" Value='" + Me.Text))
      output.Write(("' Size=" + Me.Width + ">"))
   End Sub 'Render
 End Class 'MyTextBox
End Namespace 'AttributeAccessor

.NET Framework
Available since 1.1
Return to top