Export (0) Print
Expand All

TemplateDefinition Class

Provides properties and methods that define a template element in a Web server control at design time.

System.Object
  System.Web.UI.Design.DesignerObject
    System.Web.UI.Design.TemplateDefinition

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

public class TemplateDefinition : DesignerObject

The TemplateDefinition type exposes the following members.

  NameDescription
Public methodTemplateDefinition(ControlDesigner, String, Object, String)Initializes a new instance of the TemplateDefinition class, using the provided designer, template name, template, and property name.
Public methodTemplateDefinition(ControlDesigner, String, Object, String, Boolean)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.
Public methodTemplateDefinition(ControlDesigner, String, Object, String, Style)Initializes a new instance of the TemplateDefinition class, using the provided designer, template name, template, property name, and Style object.
Public methodTemplateDefinition(ControlDesigner, String, Object, String, Style, Boolean)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.
Top

  NameDescription
Public propertyAllowEditingGets a value that indicates whether the template should enable editing of its contents.
Public propertyContentGets or sets the HTML markup representing the content of the template.
Public propertyDesignerGets the associated designer component. (Inherited from DesignerObject.)
Public propertyNameGets the name of the object. (Inherited from DesignerObject.)
Public propertyPropertiesGets the object's properties. (Inherited from DesignerObject.)
Public propertyServerControlsOnlyRetrieves a value indicating whether the template should limit content to Web server controls, as set in the TemplateDefinition constructor. This property is read-only.
Public propertyStyleRetrieves the style that should be applied to the template as set in the TemplateDefinition constructor. This property is read-only.
Public propertySupportsDataBindingRetrieves or sets a value indicating whether the template supports data binding.
Public propertyTemplatedObjectRetrieves the component in which the template resides. This property is read-only.
Public propertyTemplatePropertyNameRetrieves the property name for the template that the design host should display in the property grid.
Top

  NameDescription
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.)
Protected methodGetServiceGets a service from the design host, as identified by the provided type. (Inherited from DesignerObject.)
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

  NameDescription
Explicit interface implemetationPrivate methodIServiceProvider.GetServiceFor a description of this member, see IServiceProvider.GetService. (Inherited from DesignerObject.)
Top

The 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 2005. A design host uses the properties and methods of the TemplateDefinition class to facilitate creating and editing a template at design time.

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.

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 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;
            }
        }
    }
}

.NET Framework

Supported in: 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