TemplateInstanceAttribute Klasa

Definicja

Definiuje atrybut metadanych używany do określania liczby dozwolonych wystąpień szablonu. Klasa ta nie może być dziedziczona.

public ref class TemplateInstanceAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class TemplateInstanceAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property)>]
type TemplateInstanceAttribute = class
    inherit Attribute
Public NotInheritable Class TemplateInstanceAttribute
Inherits Attribute
Dziedziczenie
TemplateInstanceAttribute
Atrybuty

Przykłady

W poniższym przykładzie kodu pokazano, jak używać wyliczenia TemplateInstance i TemplateInstanceAttribute klasy. Niestandardowa LoginView kontrolka o nazwie MyLoginViewA zastępuje AnonymousTemplate właściwość i używa TemplateInstanceAttribute klasy do określenia, że jest tworzone tylko jedno wystąpienie AnonymousTemplate właściwości.

using System;
using System.Data;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.CS.Controls
{
    public class MyLoginViewA : LoginView
    {
        private ITemplate _anonymoustemplate;

        [Browsable(false),
        DefaultValue(null),
        PersistenceMode(PersistenceMode.InnerProperty),
        TemplateContainer(typeof(LoginView)),
        TemplateInstance(TemplateInstance.Single)
        ]
        public override ITemplate AnonymousTemplate
        {
            get
            {
                return _anonymoustemplate;
            }
            set
            {
                _anonymoustemplate = value;
            }
        }
    }
}
Imports System.Data
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB.Controls

    Public Class MyLoginViewA
        Inherits LoginView

        Private _anonymoustemplate As ITemplate

        <Browsable(False), DefaultValue(""), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(GetType(LoginView)), TemplateInstance(TemplateInstance.Single)> _
        Public Overrides Property AnonymousTemplate() As System.Web.UI.ITemplate
            Get
                Return _anonymoustemplate
            End Get
            Set(ByVal value As System.Web.UI.ITemplate)
                _anonymoustemplate = value
            End Set
        End Property

    End Class

End Namespace

Poniższy przykład kodu to plik ASPX, który używa kontrolki MyLoginViewA i pokazuje, jak uzyskać dostęp do właściwości TemplateInstanceAttribute klasy.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Register TagPrefix="AspNetSamples" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS.Controls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
   
  // <Snippet3> 
  protected void Page_Load(object sender, EventArgs e)
  {
        
    // Get the class type for which to access metadata.
    Type clsType = typeof(MyLoginViewA);
    // Get the PropertyInfo object for FirstTemplate.
    PropertyInfo pInfo = clsType.GetProperty("AnonymousTemplate");
    // See if the TemplateInstanceAttribute is defined for this property.
    bool isDef = Attribute.IsDefined(pInfo, typeof(TemplateInstanceAttribute));

    // Display the result if the attribute exists.
    if (isDef)
    {
      TemplateInstanceAttribute tia =
        (TemplateInstanceAttribute)Attribute.GetCustomAttribute(pInfo, typeof(TemplateInstanceAttribute));
      Response.Write("The <AnonymousTemplate> has the TemplateInstanceAttribute = " + tia.Instances.ToString() + ".<br />");
      if (tia.IsDefaultAttribute())
        Response.Write("The TemplateInstanceAttribute used is the same as the default instance.");
      else
        Response.Write("The TemplateInstanceAttribute used is not the same as the default instance.");
    }

  }
  // </Snippet3> 

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TemplateInstance Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <AspNetSamples:MyLoginViewA id="MyLoginViewA1" runat="server">
        <AnonymousTemplate>
          <asp:Label ID="LoginViewLabel1" runat="server" Text="LoginView Anonymous Template Text"/>
        </AnonymousTemplate>
      </AspNetSamples:MyLoginViewA>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Register TagPrefix="AspNetSamples" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  ' <Snippet3>
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
    ' Get the class type for which to access metadata.
    Dim clsType As Type = GetType(MyLoginViewA)
    ' Get the PropertyInfo object for FirstTemplate.
    Dim pInfo As PropertyInfo = clsType.GetProperty("AnonymousTemplate")
    ' See if the TemplateInstanceAttribute is defined for this property.
    Dim isDef As Boolean = Attribute.IsDefined(pInfo, GetType(TemplateContainerAttribute))
    
    ' Display the result if the attribute exists.
    If isDef Then
      Dim tia As TemplateInstanceAttribute = CType(Attribute.GetCustomAttribute(pInfo, GetType(TemplateInstanceAttribute)), TemplateInstanceAttribute)
      Response.Write("The <AnonymousTemplate> has the TemplateInstanceAttribute = " & tia.Instances.ToString() & ".<br />")
      If (tia.IsDefaultAttribute()) Then
        Response.Write("The TemplateInstanceAttribute used is the same as the default instance.")
      Else
        Response.Write("The TemplateInstanceAttribute used is not the same as the default instance.")
      End If

    End If

  End Sub
  ' </Snippet3>
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TemplateInstance Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <AspNetSamples:MyLoginViewA id="MyLoginViewA1" runat="server">
        <AnonymousTemplate>
          <asp:Label ID="LoginViewLabel1" runat="server" Text="LoginView Anonymous Template Text"/>
        </AnonymousTemplate>
      </AspNetSamples:MyLoginViewA>
    </div>
    </form>
</body>
</html>

Uwagi

Klasa TemplateInstanceAttribute umożliwia oznaczenie właściwości szablonu jako jednej lub wielu wystąpień. Szablon, który umożliwia tylko pojedyncze wystąpienie, może zawierać kontrolki, które znajdują się w nim odwołania. Właściwość ZoneTemplate jest przykładem właściwości, która może zostać utworzona tylko raz.

Ta TemplateInstanceAttribute klasa jest opcjonalna. Jeśli właściwość szablonu nie zostanie rozszerzona o klasę TemplateInstanceAttribute , zostanie użyta wartość domyślna Multiple , pole. Aby uzyskać więcej informacji na temat używania atrybutów, zobacz Atrybuty.

Konstruktory

TemplateInstanceAttribute(TemplateInstance)

Inicjuje TemplateInstanceAttribute nowe wystąpienie klasy z określoną TemplateInstance wartością wyliczenia.

Pola

Default

Definiuje wartość domyślną TemplateInstanceAttribute klasy. To pole jest tylko do odczytu.

Multiple

Tworzy wystąpienie TemplateInstanceAttribute klasy jako jedno reprezentujące szablon, który zostanie utworzony wielokrotnie. To pole jest tylko do odczytu.

Single

Tworzy wystąpienie TemplateInstanceAttribute klasy jako jedno reprezentujące szablon, który zostanie utworzony pojedynczo. To pole jest tylko do odczytu.

Właściwości

Instances

TemplateInstance Pobiera wartość wyliczenia, którą reprezentuje bieżące wystąpienie szablonu.

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego elementu Attribute.

(Odziedziczone po Attribute)

Metody

Equals(Object)

Wskazuje, czy określony obiekt jest obiektem i jest identyczny z tym TemplateInstanceAttribute obiektemTemplateInstanceAttribute.

GetHashCode()

Pobiera kod skrótu dla tego TemplateInstanceAttribute obiektu.

GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
IsDefaultAttribute()

Zwraca wartość wskazującą, czy bieżący TemplateInstanceAttribute obiekt jest taki sam jak obiekt domyślny TemplateInstanceAttribute .

Match(Object)

W przypadku zastąpienia w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie obiektu, którego można użyć do pobrania informacji o typie interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy

Zobacz też