TemplateInstance TemplateInstance TemplateInstance TemplateInstance Enum

定义

指定模板的实例可创建的次数。Specifies how many times an instance of a template can be created.

public enum class TemplateInstance
public enum TemplateInstance
type TemplateInstance = 
Public Enum TemplateInstance
继承
TemplateInstanceTemplateInstanceTemplateInstanceTemplateInstance

字段

Multiple Multiple Multiple Multiple 0

可实例化多次的模板。A template that is instantiated multiple times.

Single Single Single Single 1

只可实例化一次的模板。A template that is instantiated only one time.

示例

下面的代码示例演示如何使用TemplateInstance枚举和TemplateInstanceAttribute类。The following code example demonstrates how to use the TemplateInstance enumeration and the TemplateInstanceAttribute class. 自定义LoginView控件,名为MyLoginView,重写AnonymousTemplate属性,并使用TemplateInstanceAttribute类,以指定只有一个实例的AnonymousTemplate创建属性。A custom LoginView control, named MyLoginView, overrides the AnonymousTemplate property and uses the TemplateInstanceAttribute class to specify that only one instance of the AnonymousTemplate property is created.

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 MyLoginView : 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 Microsoft.VisualBasic
Imports System
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 MyLoginView
        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 文件MyLoginView控件并演示如何访问Label中控件AnonymousTemplate属性。The following code example is an ASPX file that uses the MyLoginView control and demonstrates how to access a Label control that is inside the AnonymousTemplate property.

<%@ Page Language="C#" %>
<%@ 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">
    
  protected void Page_Load(object sender, EventArgs e)
  {
    this.DataBind();
    this.LoginViewLabel1.Text = "LoginView Anonymous Template Label Set Dynamically.";    
  }
</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:MyLoginView id="MyLoginView1" runat="server">
        <AnonymousTemplate>
          <asp:Label ID="LoginViewLabel1" runat="server" Text="Test"/>
        </AnonymousTemplate>
      </AspNetSamples:MyLoginView>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ 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">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
    Me.DataBind()
    Me.LoginViewLabel1.Text = "LoginView Anonymous Template Label Set Dynamically."

  End Sub
  
</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:MyLoginView id="MyLoginView1" runat="server">
        <AnonymousTemplate>
          <asp:Label ID="LoginViewLabel1" runat="server" Text="Test"/>
        </AnonymousTemplate>
      </AspNetSamples:MyLoginView>
    </div>
    </form>
</body>
</html>

注解

TemplateInstance枚举指定值,该值指示在创建模板的实例的次数。The TemplateInstance enumeration specifies values indicating the number of times an instance of a template can be created. TemplateInstanceAttribute类使用中的值TemplateInstanceAttribute枚举。The TemplateInstanceAttribute class uses values from the TemplateInstanceAttribute enumeration. 具体而言,SingleMultiple字段分别指定单个和多个实例的模板。In particular, the Single and Multiple fields specify single and multiple instances of a template, respectively. 单个实例的模板,可引用模板内包含的控件。A single instance of a template allows you to reference controls that are contained within the template.

在属性元数据中使用的单个值的控件的示例包括ZoneTemplate属性CatalogZone控件,ZoneTemplate属性EditorZone控件,并ZoneTemplate属性WebPartZoneExamples of controls that use the Single value in property metadata include the ZoneTemplate property of the CatalogZone control, the ZoneTemplate property of the EditorZone control, and the ZoneTemplate property of the WebPartZone.

适用于

另请参阅