TemplateInstanceAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义一个元数据特性,该元数据特性用于指定所允许的模板的实例数。 此类不能被继承。
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 AttributePublic NotInheritable Class TemplateInstanceAttribute
Inherits Attribute- 继承
- 属性
示例
下面的代码示例演示如何使用 TemplateInstance 枚举和 TemplateInstanceAttribute 类。 名为 的MyLoginViewA自定义LoginView控件将重写 AnonymousTemplate 属性,并使用 TemplateInstanceAttribute 类指定仅创建属性的AnonymousTemplate一个实例。
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
下面的代码示例是一个 ASPX 文件,它使用 MyLoginViewA 控件并演示如何访问 类的属性 TemplateInstanceAttribute 。
<%@ 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>
注解
类 TemplateInstanceAttribute 允许将模板属性标记为允许单个或多个实例化的模板属性。 仅允许单个实例化的模板可以引用其中包含的控件。 属性 ZoneTemplate 是只能实例化一次的属性的示例。
此类 TemplateInstanceAttribute 是可选的。 如果未使用 TemplateInstanceAttribute 类扩展模板属性,则使用默认值 Multiple 字段。 有关使用特性的详细信息,请参阅 特性。
构造函数
| TemplateInstanceAttribute(TemplateInstance) | 使用指定的 TemplateInstance 枚举值初始化 TemplateInstanceAttribute 类的新实例。 | 
字段
| Default | 定义 TemplateInstanceAttribute 类的默认值。 此字段为只读。 | 
| Multiple | 将 TemplateInstanceAttribute 类的实例创建为表示会实例化多次的模板的实例。 此字段为只读。 | 
| Single | 将 TemplateInstanceAttribute 类的实例创建为表示会实例化一次的模板的实例。 此字段为只读。 | 
属性
| Instances | 获取当前模板实例表示的 TemplateInstance 枚举值。 | 
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
方法
| Equals(Object) | 指示指定对象是否为 TemplateInstanceAttribute 对象以及是否与此 TemplateInstanceAttribute 对象相同。 | 
| GetHashCode() | 获取此 TemplateInstanceAttribute 对象的哈希代码。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 返回一个值,该值指示当前 TemplateInstanceAttribute 对象是否与默认 TemplateInstanceAttribute 对象相同。 | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |