Export (0) Print
Expand All

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.

For a list of all members of this type, see IAttributeAccessor Members.

[Visual Basic]
Public Interface IAttributeAccessor
[C#]
public interface IAttributeAccessor
[C++]
public __gc __interface IAttributeAccessor
[JScript]
public interface IAttributeAccessor

Classes that Implement IAttributeAccessor

Class Description
HtmlControl Defines the methods, properties, and events common to all HTML server controls in the Web Forms page framework.
ListItem Represents a data item in a data-bound list control. This class cannot be inherited.
UserControl Represents an .ascx file, also known as a user control, requested from a server that hosts an ASP.NET Web application. The file must be called from a Web Forms page or a parser error will occur.
WebControl Serves as the base class that defines the methods, properties and events common to all controls in the System.Web.UI.WebControls namespace.

Remarks

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 since 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.

Example

[Visual Basic] 
' 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.UI

Namespace AttributeAccessor

 Public Class MyTextBox
   Inherits Control
   Implements IAttributeAccessor 

   ' Declare the Width property.   
   Public Property Width() As String
      Get
         Return CType(ViewState("Width"), String)
      End Get
      Set
         ViewState("Width") = value
      End Set
   End Property
   
   ' Declare the Text property.
   
   Public Property Text() As String
      Get
         Return CType(ViewState("Text"), String)
      End Get
      Set
         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

[C#] 
// 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.
using System;
using System.Web.UI;

namespace AttributeAccessor
{
   public class MyTextBox : Control, IAttributeAccessor
   {
      // Declare the Width property.
      public String Width
      {
         get
         {
            return (String)ViewState["Width"];
         }
         set
         {
            ViewState["Width"] = value;
         }
      }

      // Declare the Text property.
      public String Text
      {
         get
         {
            return (String)ViewState["Text"];
         }
         set
         {
            ViewState["Text"] = value;
         }
      }
      // 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 void SetAttribute(String name, String value1)
      {
         ViewState[name] = value1;
      }

      // 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 String GetAttribute(String name)
      {
         return (String)ViewState[name];
      }

      protected override void Render(HtmlTextWriter output)
      {
         output.Write("<input type=text id= " + this.UniqueID);
         output.Write(" Value='" + this.Text);
         output.Write("' Size=" + this.Width + ">");
      }
   }
}

[C++] 
// 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.
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
using namespace System;
using namespace System::Web::UI;

public __gc class MyTextBox : public Control, public IAttributeAccessor {
   // Declare the Width property.
public:
__property String* get_Width() {
         return dynamic_cast<String*>(ViewState->Item[S"Width"]);
      }
__property void set_Width(String* value) {
         ViewState->Item[S"Width"] = value;
      }


   // Declare the Text property.
__property String* get_Text() {
         return dynamic_cast<String*>(ViewState->Item[S"Text"]);
      }
__property void set_Text(String* value) {
         ViewState->Item[S"Text"] = value;
      }

   // 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:
   void SetAttribute(String* name, String* value1) {
      ViewState->Item[name] = value1;
   }

   // 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:
   String* GetAttribute(String* name) {
      return dynamic_cast<String*>(ViewState->Item[name]);
   }

protected:
   void Render(HtmlTextWriter* output) {
      output->Write(S"<input type=text id= {0}", this->UniqueID);
      output->Write(S" Value=' {0}", this->Text);
      output->Write(String::Concat(S"' Size= ", this->Width, S">"));
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

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

See Also

IAttributeAccessor Members | System.Web.UI Namespace | HtmlControl | WebControl

Show:
© 2014 Microsoft