Export (0) Print
Expand All

TemplateGroup Class

A collection of TemplateDefinition objects representing the template elements in a Web server control at design time.

System.Object
  System.Web.UI.Design.TemplateGroup

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

'Declaration
Public Class TemplateGroup

The TemplateGroup type exposes the following members.

  NameDescription
Public methodTemplateGroup(String)Initializes a new instance of the TemplateGroup class, using the provided name.
Public methodTemplateGroup(String, Style)Initializes a new instance of the TemplateGroup class, using the provided name and style.
Top

  NameDescription
Public propertyGroupNameGets the name of the group.
Public propertyGroupStyleGets the current style for the group.
Public propertyIsEmptyGets a value indicating whether there are any templates in the group.
Public propertyTemplatesGets an array of all template definitions in the group.
Top

  NameDescription
Public methodAddTemplateDefinitionAdds the provided TemplateDefinition to the group.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The following code example demonstrates how to create a control designer class that derives from the ControlDesigner class. This control designer supports a control with four possible templates.

To try it, compile the code, and then, in a design host such as Visual Studio 2005, look at the page in Design view. Select the control, click the action list to select a template to modify, and then use the drag-and-drop feature to move controls into the template.

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.Design

Namespace ASPNet.Design.Samples

    ' Set an attribute reference to the designer, and define  
    ' the HTML markup that the toolbox will write into the source.
    <Designer(GetType(TemplateGroupsSampleDesigner)), _
        ToolboxData("<{0}:TemplateGroupsSample runat=server></{0}:TemplateGroupsSample>")> _
    Public Class TemplateGroupsSample
        Inherits WebControl
        Implements INamingContainer

        ' Field for the templates 
        Private _templates() As ITemplate

        ' Constructor 
        Public Sub New()
            ReDim _templates(4)
        End Sub 

        ' For each template property, set the designer attributes  
        ' so the property does not appear in the property grid, but  
        ' changes to the template are persisted in the control.
        <Browsable(False), _
            PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property Template1() As ITemplate
            Get 
                Return _templates(0)
            End Get 
            Set(ByVal Value As ITemplate)
                _templates(0) = Value
            End Set 
        End Property
        <Browsable(False), _
            PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property Template2() As ITemplate
            Get 
                Return _templates(1)
            End Get 
            Set(ByVal Value As ITemplate)
                _templates(1) = Value
            End Set 
        End Property
        <Browsable(False), _
            PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property Template3() As ITemplate
            Get 
                Return _templates(2)
            End Get 
            Set(ByVal Value As ITemplate)
                _templates(2) = Value
            End Set 
        End Property
        <Browsable(False), _
            PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property Template4() As ITemplate
            Get 
                Return _templates(3)
            End Get 
            Set(ByVal Value As ITemplate)
                _templates(3) = Value
            End Set 
        End Property 

        Protected Overrides Sub CreateChildControls()
            ' Instantiate the template inside the panel 
            ' then add the panel to the Controls collection 
            Dim i As Integer 

            For i = 0 To 3
                Dim pan As New Panel()
                _templates(i).InstantiateIn(pan)
                Me.Controls.Add(pan)
            Next 
        End Sub 

    End Class 

    ' Designer for the TemplateGroupsSample class 
    Public Class TemplateGroupsSampleDesigner
        Inherits System.Web.UI.Design.ControlDesigner

        Private col As TemplateGroupCollection = Nothing 

        Public Overrides Sub Initialize(ByVal Component As IComponent)
            ' Initialize the base 
            MyBase.Initialize(Component)
            ' Turn on template editing
            SetViewFlags(ViewFlags.TemplateEditing, True)
        End Sub 

        ' Add instructions to the placeholder view of the control 
        Public Overloads Overrides Function GetDesignTimeHtml() As String 
            Return CreatePlaceHolderDesignTimeHtml("Click here and use " & _
                "the task menu to edit the templates.")
        End Function 

        Public Overrides ReadOnly Property TemplateGroups() As TemplateGroupCollection
            Get 
                If IsNothing(col) Then 
                    ' Get the base collection
                    col = MyBase.TemplateGroups

                    ' Create variables 
                    Dim tempGroup As TemplateGroup
                    Dim tempDef As TemplateDefinition
                    Dim ctl As TemplateGroupsSample

                    ' Get reference to the component as TemplateGroupsSample
                    ctl = CType(Component, TemplateGroupsSample)

                    ' Create a TemplateGroup
                    tempGroup = New TemplateGroup("Template Set A")

                    ' Create a TemplateDefinition
                    tempDef = New TemplateDefinition(Me, "Template A1", ctl, "Template1", True)

                    ' Add the TemplateDefinition to the TemplateGroup
                    tempGroup.AddTemplateDefinition(tempDef)

                    ' Create another TemplateDefinition
                    tempDef = New TemplateDefinition(Me, "Template A2", ctl, "Template2", True)

                    ' Add the TemplateDefinition to the TemplateGroup
                    tempGroup.AddTemplateDefinition(tempDef)

                    ' Add the TemplateGroup to the TemplateGroupCollection
                    col.Add(tempGroup)

                    ' Create another TemplateGroup and populate it
                    tempGroup = New TemplateGroup("Template Set B")
                    tempDef = New TemplateDefinition(Me, "Template B1", ctl, "Template3", True)
                    tempGroup.AddTemplateDefinition(tempDef)
                    tempDef = New TemplateDefinition(Me, "Template B2", ctl, "Template4", True)
                    tempGroup.AddTemplateDefinition(tempDef)

                    ' Add the TemplateGroup to the TemplateGroupCollection
                    col.Add(tempGroup)
                End If 

                Return col
            End Get 
        End Property 

        ' Do not allow direct resizing unless in TemplateMode 
        Public Overrides ReadOnly Property AllowResize() As Boolean 
            Get 
                If Me.InTemplateMode Then 
                    Return True 
                Else 
                    Return False 
                End If 
            End Get 
        End Property 
    End Class 
End Namespace


...


<%@ Page Language="VB" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="ASPNet.Design.Samples" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

       <aspSample:TemplateGroupsSample runat="server" ID="TGSample1">
       </aspSample:TemplateGroupsSample>

    </div>
    </form>
</body>
</html>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft