Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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

The IAttributeAccessor type exposes the following members.

  NameDescription
Public methodGetAttributeWhen implemented by a class, retrieves the specified attribute property from the server control.
Public methodSetAttributeWhen implemented by a class, designates an attribute and its value to assign to the ASP.NET server control.
Top

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. 
using System;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;

namespace AttributeAccessor
{
   [AspNetHostingPermission(SecurityAction.Demand, 
      Level=AspNetHostingPermissionLevel.Minimal)]
   public sealed 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 + ">");
      }
   }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.