IAttributeAccessor 介面

定義

定義 ASP.NET 伺服器控制項所使用的方法,提供任何在伺服器控制項的開頭標記中宣告之屬性的程式設計存取。

public interface class IAttributeAccessor
public interface IAttributeAccessor
type IAttributeAccessor = interface
Public Interface IAttributeAccessor
衍生

範例

// 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 + ">");
      }
   }
}
' 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.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
      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
   
   ' 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
 End Class
End Namespace 'AttributeAccessor

備註

如果您撰寫繼承自、 HtmlControlListItem 類別的WebControl自定義伺服器控制項,.NET Framework 會自動提供屬性的程式設計存取,因為這些類別都會實作 IAttributeAccessor 介面。

如果您撰寫不繼承自這些類別的自定義伺服器控件,並計劃允許以程式設計方式存取未與控件強型別屬性對應的屬性,請務必實 IAttributeAccessor 作 介面。

方法

GetAttribute(String)

當類別實作時,從伺服器控制項擷取指定的屬性內容。

SetAttribute(String, String)

當類別實作時,指定屬性和屬性值,以指派給 ASP.NET 伺服器控制項。

適用於

另請參閱