IAttributeAccessor IAttributeAccessor IAttributeAccessor IAttributeAccessor Interface

定義

サーバー コントロールの開始タグで宣言された任意の属性にプログラムによってアクセスするために、ASP.NET サーバー コントロールで使用されるメソッドを定義します。Defines methods used by ASP.NET server controls to provide programmatic access to any attribute declared in the opening tag of a server control.

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 'MyTextBox
End Namespace 'AttributeAccessor

注釈

WebControlIAttributeAccessor 、またはListItemクラスを継承するカスタムサーバーコントロールを作成する場合、これらの各クラスは、次のものを実装しているため、属性へのプログラムによるアクセスが .NET Framework によって自動的に提供されます。 HtmlControlefi.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.

これらのクラスの1つを継承しないカスタムサーバーコントロールを作成し、そのコントロールの厳密に型指定されたプロパティに対応しない属性へのプログラムによるアクセスを許可IAttributeAccessorする場合は、必ずインターフェイスを実装してください。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.

メソッド

GetAttribute(String) GetAttribute(String) GetAttribute(String) GetAttribute(String)

クラスによって実装された場合は、指定した属性プロパティをサーバー コントロールから取得します。When implemented by a class, retrieves the specified attribute property from the server control.

SetAttribute(String, String) SetAttribute(String, String) SetAttribute(String, String) SetAttribute(String, String)

クラスによって実装された場合は、属性とその値を指定し、ASP.NET サーバー コントロールに割り当てます。When implemented by a class, designates an attribute and its value to assign to the ASP.NET server control.

適用対象

こちらもご覧ください