TemplateDefinition TemplateDefinition TemplateDefinition TemplateDefinition Class

定義

提供在設計階段定義 Web 伺服器控制項中樣板項目的屬性和方法。Provides properties and methods that define a template element in a Web server control at design time.

public ref class TemplateDefinition : System::Web::UI::Design::DesignerObject
public class TemplateDefinition : System.Web.UI.Design.DesignerObject
type TemplateDefinition = class
    inherit DesignerObject
Public Class TemplateDefinition
Inherits DesignerObject
繼承
TemplateDefinitionTemplateDefinitionTemplateDefinitionTemplateDefinition

範例

下列程式碼範例示範如何衍生自訂類別從ControlDesigner類別。The following code example demonstrates how to derive a custom class from the ControlDesigner class. 此控制項設計工具會支援具有四個可能的範本的控制項。This control designer supports a control with four possible templates.

若要試試看,System.Design.dll 組件的參考、 編譯程式碼,加上然後,在設計裝載 例如Visual Studio 2005Visual Studio 2005,查看 [設計] 檢視中的頁面。To try it, add a reference to the System.Design.dll assembly, compile the code, and then, in a design host such as Visual Studio 2005Visual 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.

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using 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(typeof(TemplateGroupsSampleDesigner)),
        ToolboxData("<{0}:TemplateGroupsSample runat=server></{0}:TemplateGroupsSample>")]
    public sealed class TemplateGroupsSample : WebControl, INamingContainer
    {
        // Field for the templates
        private ITemplate[] _templates;

        // Constructor
        public TemplateGroupsSample()
        {
            _templates = new ITemplate[4];
        }

        // 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 ITemplate Template1
        {
            get { return _templates[0]; }
            set { _templates[0] = value; }
        }
        [Browsable(false),
            PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate Template2
        {
            get { return _templates[1]; }
            set { _templates[1] = value; }
        }
        [Browsable(false),
            PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate Template3
        {
            get { return _templates[2]; }
            set { _templates[2] = value; }
        }
        [Browsable(false),
            PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate Template4
        {
            get { return _templates[3]; }
            set { _templates[3] = value; }
        }

        protected override void CreateChildControls()
        {
            // Instantiate each template inside a panel
            // then add the panel to the Controls collection
            for (int i = 0; i < 4; i++)
            {
                Panel pan = new Panel();
                _templates[i].InstantiateIn(pan);
                this.Controls.Add(pan);
            }
        }
    }

    // Designer for the TemplateGroupsSample control
    public class TemplateGroupsSampleDesigner : ControlDesigner
    {
        TemplateGroupCollection col = null;

        public override void Initialize(IComponent component)
        {
            // Initialize the base
            base.Initialize(component);
            // Turn on template editing
            SetViewFlags(ViewFlags.TemplateEditing, true);
        }

        // Add instructions to the placeholder view of the control
        public override string GetDesignTimeHtml()
        {
            return CreatePlaceHolderDesignTimeHtml("Click here and use " +
                "the task menu to edit the templates.");
        }

        public override TemplateGroupCollection TemplateGroups
        {
            get
            {

                if (col == null)
                {
                    // Get the base collection
                    col = base.TemplateGroups;

                    // Create variables
                    TemplateGroup tempGroup;
                    TemplateDefinition tempDef;
                    TemplateGroupsSample ctl;

                    // Get reference to the component as TemplateGroupsSample
                    ctl = (TemplateGroupsSample)Component;

                    // Create a TemplateGroup
                    tempGroup = new TemplateGroup("Template Set A");

                    // Create a TemplateDefinition
                    tempDef = new TemplateDefinition(this, "Template A1", 
                        ctl, "Template1", true);

                    // Add the TemplateDefinition to the TemplateGroup
                    tempGroup.AddTemplateDefinition(tempDef);

                    // Create another TemplateDefinition
                    tempDef = new TemplateDefinition(this, "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(this, "Template B1", 
                        ctl, "Template3", true);
                    tempGroup.AddTemplateDefinition(tempDef);
                    tempDef = new TemplateDefinition(this, "Template B2", 
                        ctl, "Template4", true);
                    tempGroup.AddTemplateDefinition(tempDef);

                    // Add the TemplateGroup to the TemplateGroupCollection
                    col.Add(tempGroup);
                }

                return col;
            }
        }

        // Do not allow direct resizing unless in TemplateMode
        public override bool AllowResize
        {
            get
            {
                if (this.InTemplateMode)
                    return true;
                else
                    return false;
            }
        }
    }
}
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>

備註

TemplateDefinition類別提供基底範本定義類別可以繼承自並擴充控制項設計工具將用於在設計主應用程式中的樣板化控制項提供支援,例如Visual Studio 2005Visual Studio 2005The TemplateDefinition class provides a base template definition class that can be inherited from and extended for a control designer to use in providing support for templated controls in a design host such as Visual Studio 2005Visual Studio 2005. 設計主應用程式使用的屬性和方法TemplateDefinition類別來簡化建立和編輯範本在設計階段。A design host uses the properties and methods of the TemplateDefinition class to facilitate creating and editing a template at design time.

建構函式

TemplateDefinition(ControlDesigner, String, Object, String) TemplateDefinition(ControlDesigner, String, Object, String) TemplateDefinition(ControlDesigner, String, Object, String) TemplateDefinition(ControlDesigner, String, Object, String)

使用所提供的設計工具、樣板名稱、樣板和屬性名稱,初始化 TemplateDefinition 類別的新執行個體。Initializes a new instance of the TemplateDefinition class, using the provided designer, template name, template, and property name.

TemplateDefinition(ControlDesigner, String, Object, String, Boolean) TemplateDefinition(ControlDesigner, String, Object, String, Boolean) TemplateDefinition(ControlDesigner, String, Object, String, Boolean) TemplateDefinition(ControlDesigner, String, Object, String, Boolean)

使用所提供的設計工具、樣板名稱、樣板、屬性名稱和是否將樣板內容限制為 Web 伺服器控制項,初始化 TemplateDefinition 類別的新執行個體。Initializes a new instance of the TemplateDefinition class, using the provided designer, template name, template, property name, and whether to limit the template contents to Web server controls.

TemplateDefinition(ControlDesigner, String, Object, String, Style) TemplateDefinition(ControlDesigner, String, Object, String, Style) TemplateDefinition(ControlDesigner, String, Object, String, Style) TemplateDefinition(ControlDesigner, String, Object, String, Style)

使用所提供的設計工具、樣板名稱、樣板、屬性名稱和 TemplateDefinition 物件,初始化 Style 類別的新執行個體。Initializes a new instance of the TemplateDefinition class, using the provided designer, template name, template, property name, and Style object.

TemplateDefinition(ControlDesigner, String, Object, String, Style, Boolean) TemplateDefinition(ControlDesigner, String, Object, String, Style, Boolean) TemplateDefinition(ControlDesigner, String, Object, String, Style, Boolean) TemplateDefinition(ControlDesigner, String, Object, String, Style, Boolean)

使用所提供的設計工具、樣板名稱、樣板、屬性名稱、TemplateDefinition 物件和是否將內容限制為 Web 伺服器控制項,初始化 Style 類別的新執行個體。Initializes a new instance of the TemplateDefinition class, using the provided designer, template name, template, property name, Style object, and whether to limit content to Web server controls.

屬性

AllowEditing AllowEditing AllowEditing AllowEditing

取得值,這個值表示樣板是否應允許編輯其內容。Gets a value that indicates whether the template should enable editing of its contents.

Content Content Content Content

取得或設定 HTML 標記,表示樣板的內容。Gets or sets the HTML markup representing the content of the template.

Designer Designer Designer Designer

取得關聯的設計工具元件。Gets the associated designer component.

(Inherited from DesignerObject)
Name Name Name Name

取得物件名稱。Gets the name of the object.

(Inherited from DesignerObject)
Properties Properties Properties Properties

取得物件的屬性。Gets the object's properties.

(Inherited from DesignerObject)
ServerControlsOnly ServerControlsOnly ServerControlsOnly ServerControlsOnly

擷取值,指出樣板是否應將內容限制為 Web 伺服器控制項 (如同 TemplateDefinition 建構函式 (Constructor) 所設定)。Retrieves a value indicating whether the template should limit content to Web server controls, as set in the TemplateDefinition constructor. 這個屬性是唯讀的。This property is read-only.

Style Style Style Style

擷取樣式,該樣式應套用至樣板 (如同 TemplateDefinition 建構函式所設定)。Retrieves the style that should be applied to the template as set in the TemplateDefinition constructor. 這個屬性是唯讀的。This property is read-only.

SupportsDataBinding SupportsDataBinding SupportsDataBinding SupportsDataBinding

擷取或設定值,指出樣板是否支援資料繫結 (Data Binding)。Retrieves or sets a value indicating whether the template supports data binding.

TemplatedObject TemplatedObject TemplatedObject TemplatedObject

擷取樣板所在的元件。Retrieves the component in which the template resides. 這個屬性是唯讀的。This property is read-only.

TemplatePropertyName TemplatePropertyName TemplatePropertyName TemplatePropertyName

擷取設計主應用程式應在屬性方格中顯示之樣板的屬性名稱。Retrieves the property name for the template that the design host should display in the property grid.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetService(Type) GetService(Type) GetService(Type) GetService(Type)

從設計主應用程式取得服務,由所提供的型別識別。Gets a service from the design host, as identified by the provided type.

(Inherited from DesignerObject)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

IServiceProvider.GetService(Type) IServiceProvider.GetService(Type) IServiceProvider.GetService(Type) IServiceProvider.GetService(Type)

如需這個成員的說明,請參閱 GetService(Type)For a description of this member, see GetService(Type).

(Inherited from DesignerObject)

適用於

另請參閱