IAttributeAccessor Interfejs

Definicja

Definiuje metody używane przez kontrolki serwera ASP.NET w celu zapewnienia programowego dostępu do dowolnego atrybutu zadeklarowanego w tagu otwierania kontrolki serwera.

C#
public interface IAttributeAccessor
Pochodne

Przykłady

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;
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 + ">");
      }
   }
}

Uwagi

Jeśli tworzysz niestandardową kontrolkę serwera dziedziczą z WebControlklasy , HtmlControllub ListItem , program .NET Framework automatycznie zapewnia programowy dostęp do atrybutów, ponieważ każda z tych klas implementuje IAttributeAccessor interfejs.

Jeśli tworzysz niestandardową kontrolkę serwera, która nie dziedziczy z jednej z tych klas i planujesz zezwolić na programowy dostęp do atrybutów, które nie odpowiadają silnie typizowanym właściwościom kontrolki, pamiętaj o zaimplementowaniu interfejsu IAttributeAccessor .

Metody

GetAttribute(String)

Po zaimplementowaniu przez klasę pobiera określoną właściwość atrybutu z kontrolki serwera.

SetAttribute(String, String)

Po zaimplementowaniu przez klasę wyznacza atrybut i jego wartość do przypisania do kontroli serwera ASP.NET.

Dotyczy

Produkt Wersje
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Zobacz też