TemplateInstance Enumeration

 

Specifies how many times an instance of a template can be created.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Public Enumeration TemplateInstance

Member nameDescription
Multiple

A template that is instantiated multiple times.

Single

A template that is instantiated only one time.

The TemplateInstance enumeration specifies values indicating the number of times an instance of a template can be created. The TemplateInstanceAttribute class uses values from the TemplateInstance enumeration. 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.

Examples 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.

The following code example demonstrates how to use the TemplateInstance enumeration and the TemplateInstanceAttribute class. 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.

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

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="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>

.NET Framework
Available since 2.0
Return to top
Show: