How to: Use Services and Action Lists with Control Designers

The following code example illustrates a set of custom ASP.NET controls with associated control designers. The control designers show how to implement editable regions and action lists, and how to interact with host services.

This example is described in detail in Sample Control Designer with Action Lists and Services.

Example

<?xml version="1.0" encoding="utf-8" ?>
<styles>
    <style name="BackColor" value="red"></style>
    <style name="ForeColor" value="white"></style>
</styles>

<?xml version="1.0" encoding="utf-8" ?>
<styles>
    <style name="BackColor" value="blue"></style>
    <style name="ForeColor" value="white"></style>
</styles>

<?xml version="1.0" encoding="utf-8" ?>
<styles>
    <style name="BackColor" value="green"></style>
    <style name="ForeColor" value="white"></style>
</styles>
<%@ page language="VB" %>
<%@ register tagprefix="aspSample" 
    assembly="DesignerServicesAndListsVB" 
    namespace="Samples.AspNet.VB.Controls" %>
<!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 id="Head1" runat="server">
    <title>Designer Samples</title>
</head>

<body>
  <form id="form1" runat="server">
      <p style="font-family:tahoma;font-size:larger;
      font-weight:bold">
        Using Action Lists and Designer Host Services</p>
      <div style="font-family:tahoma;font-size:x-small">
        <span style="font-size: 10pt">Control #1 (ControlWithStyleTasksDesigner):
        PanelContainerDesigner using a DesignerActionList, which
        obtains a list of XML files in the project and sets 
        the style using the XML element
        definitions.</span><p />
      </div>
      <aspSample:ControlWithStyleTasks id="ctl1" 
        runat="server" 
        backcolor="Red" forecolor="White">
          Hello there.</aspSample:ControlWithStyleTasks>
      <br />
      <div style="font-family:tahoma;font-size:x-small">
        <span style="font-size: 10pt">Control #2 (ControlWithConfigurationSettingDesigner):
        PanelContainerDesigner using configuration settings
        to obtain 
        the FrameCaption value.</span><p />
      </div>
      <aspSample:ControlWithConfigurationSetting 
        id="ctl2" runat="server">
        Hello There
      </aspSample:ControlWithConfigurationSetting>
      <br />
      <div style="font-family:tahoma;font-size:x-small">
        <span style="font-size: 10pt">Control #3 (ControlWithButtonTasksDesigner):
        PanelContainerDesigner using a smart-task action 
        item to insert a new button to the Web Form.</span><p />
      </div>
      <aspSample:ControlWithButtonTasks 
        id="ctl3" runat="server">
        Hello There
      </aspSample:ControlWithButtonTasks>
      &nbsp; &nbsp;
  </form>
</body>
</html>
<%@ page language="C#" %>
<%@ register tagprefix="aspSample" 
    assembly="DesignerServicesAndListsCS"namespace="Samples.AspNet.CS.Controls" %>
<!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 id="Head1" runat="server">
    <title>Designer Samples</title>
</head>

<body>
  <form id="form1" runat="server">
      <p style="font-family:tahoma;font-size:larger;
      font-weight:bold">
        Using Action Lists and Designer Host Services</p>
      <div style="font-family:tahoma;font-size:x-small">
        <span style="font-size: 10pt">Control #1 (ControlWithStyleTasksDesigner):
        PanelContainerDesigner using a DesignerActionList, which
        obtains a list of XML files in the project and sets 
        the style using the XML element
        definitions.</span><p />
      </div>
      <aspSample:ControlWithStyleTasks id="ctl1" 
        runat="server" 
        backcolor="Red" forecolor="White">
          Hello there.</aspSample:ControlWithStyleTasks>
      <br />
      <div style="font-family:tahoma;font-size:x-small">
        <span style="font-size: 10pt">Control #2 (ControlWithConfigurationSettingDesigner):
        PanelContainerDesigner using configuration settings
        to obtain 
        the FrameCaption value.</span><p />
      </div>
      <aspSample:ControlWithConfigurationSetting 
        id="ctl2" runat="server">
        Hello There
      </aspSample:ControlWithConfigurationSetting>
      <br />
      <div style="font-family:tahoma;font-size:x-small">
        <span style="font-size: 10pt">Control #3 (ControlWithButtonTasksDesigner):
        PanelContainerDesigner using a smart-task action 
        item to insert a new button to the Web Form.</span><p />
      </div>
      <aspSample:ControlWithButtonTasks 
        id="ctl3" runat="server">
        Hello There
      </aspSample:ControlWithButtonTasks>
      &nbsp; &nbsp;
  </form>
</body>
</html>
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.Design.WebControls
Imports System.Web.UI.WebControls
Imports System.Reflection
Imports System.Xml
Imports System.Configuration

Namespace Samples.AspNet.VB.Controls

    ' This region-based control renders the content of an XML file.
    <Designer(GetType(ControlWithStyleTasksDesigner))> _
    <ToolboxData("<{0}:ControlWithStyleTasks runat=server>" _
                 & "</{0}:ControlWithStyleTasks>")> _
    PublicClass ControlWithStyleTasks
        Inherits Panel
    EndClass

    <Designer(GetType(ControlWithConfigurationSettingDesigner))> _
    <ToolboxData("<{0}:ControlWithConfigurationSetting runat=server " _
                 & "width=100%></{0}:ControlWithConfigurationSetting>")> _
    PublicClass ControlWithConfigurationSetting
        Inherits Panel
    EndClass

    <Designer(GetType(ControlWithButtonTasksDesigner))> _
    <ToolboxData("<{0}:ControlWithButtonTasks runat=server " _
                 & "width=100%></{0}:ControlWithButtonTasks>")> _
    PublicClass ControlWithButtonTasks
        Inherits Panel
    EndClass
    ' Declare a custom button to add dynamically at design time.PublicClass NewButton
        Inherits Button

        PublicSubNew()
            [Text] = "NewButton"EndSubEndClass
    ' This control designer is used to read xml files from the project.
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    PublicClass ControlWithStyleTasksDesigner
        Inherits PanelContainerDesigner
        Private _style As Style = Nothing
        ' Add the caption by default. Note that the caption will         ' only appear if the Web server control allows         ' child controls rather than properties. PublicOverridesReadOnlyProperty FrameCaption() _
          AsStringGetReturn"Container getting styles from the project item."EndGetEndPropertyPublicOverridesReadOnlyProperty FrameStyle() As Style
            GetIf _style IsNothingThen
                    _style = New Style()
                    _style.Font.Name = "Verdana"
                    _style.Font.Size = New FontUnit("XSmall")
                    _style.BackColor = Color.LightGreen
                    _style.ForeColor = Color.Black
                EndIfReturn _style
            EndGetEndProperty
        ' Create a convenience field for the control.Private myControl As ControlWithStyleTasks

        PublicOverridesSub Initialize(ByVal component _
          As IComponent)
            ' Create the convenience control.MyBase.Initialize(component)
            myControl = CType(component, ControlWithStyleTasks)
        EndSub
        ' The following section creates a designer action list.        ' Add the specific action list for this control. The         ' procedure here depends upon the specific order for         ' the list to be added, but always add the base.PublicOverridesReadOnlyProperty ActionLists() _
          As DesignerActionListCollection

            GetDim newActionLists AsNew DesignerActionListCollection()
                newActionLists.AddRange(MyBase.ActionLists)
                newActionLists.Add(New MyList(Me))
                Return newActionLists
            EndGetEndPropertyPrivate _styleConfigPhysicalFile AsString = ""ProtectedProperty StyleConfigurationFile() AsStringGetReturn _styleConfigPhysicalFile
            EndGetSet(ByVal value AsString)
                Dim styleConfigPhysicalFilePath AsString = String.Empty
                If value.Length > 0 Then                    ' Access the folder and look for the Control.xml file;                     ' then obtain its physical path to use to set styles                     ' for this control and control designer.  Obtain the                     ' actual file and its physical path from WebApplication.Dim webApp As IWebApplication = _
                      CType(Component.Site. _
                        GetService(GetType(IWebApplication)), IWebApplication)
                    IfNot (webApp IsNothing) Then                        ' Look for the project items from the root.Dim dataFileProjectItem As IProjectItem = _
                          webApp.GetProjectItemFromUrl(("~/" + value))
                        IfNot (dataFileProjectItem IsNothing) Then
                            _styleConfigPhysicalFile = value
                            styleConfigPhysicalFilePath = _
                              dataFileProjectItem.PhysicalPath
                        EndIfEndIfEndIf
                ' Get the styles from the XML file.
                SetControlStyleFromConfiguration( _
                  styleConfigPhysicalFilePath)
            EndSetEndProperty
        ' Open the xml document and set control properties directly.PrivateSub SetControlStyleFromConfiguration(ByVal _
          path AsString)

            If path IsNothingOrElse path = String.Empty ThenReturnEndIfDim wc As WebControl = CType(Component, WebControl)
            If wc IsNothingThenReturnEndIfDim xmlDoc AsNew XmlDocument()
            xmlDoc.Load(path)

            Dim stylesList As XmlNodeList = _
              xmlDoc.GetElementsByTagName("style")
            Dim i AsIntegerFor i = 0 To stylesList.Count - 1
                If stylesList(i).Attributes("name").Value = _
                  "BackColor"ThenDim pd As PropertyDescriptor = _
                      TypeDescriptor.GetProperties(wc)("BackColor")
                    IfNot (pd IsNothing) Then
                        pd.SetValue(wc, Color.FromName(stylesList(i). _
                          Attributes("value").Value))
                    EndIfEndIfIf stylesList(i).Attributes("name").Value = _
                  "ForeColor"ThenDim pd As PropertyDescriptor = _
                      TypeDescriptor.GetProperties(wc)("ForeColor")
                    IfNot (pd IsNothing) Then
                        pd.SetValue(wc, Color.FromName(stylesList(i). _
                          Attributes("value").Value))
                    EndIfEndIfNext i
        EndSub
        ' Create the action list for this control.PrivateClass MyList
            Inherits DesignerActionList
            Private _parent As ControlWithStyleTasksDesigner

            PublicSubNew(ByVal parent As ControlWithStyleTasksDesigner)
                MyBase.New(parent.Component)
                _parent = parent
            EndSub

            <TypeConverter(GetType(FileListTypeConverter))> _
            PublicProperty ConfigureControlStyle() AsStringGetReturn _parent.StyleConfigurationFile
                EndGetSet(ByVal value AsString)
                    _parent.StyleConfigurationFile = value
                EndSetEndProperty
            ' Provide the list of sorted action items for the host.            ' Note that you can define the items through constructors             ' or through metadata on the method or property items.PublicOverridesFunction GetSortedActionItems() _
              As DesignerActionItemCollection

                Dim items AsNew DesignerActionItemCollection()

                items.Add(New DesignerActionTextItem("Configuration", "Select"))
                items.Add(New DesignerActionPropertyItem("ConfigureControlStyle", _
                                                 "Configure XML", _
                                                 "Select", _
                                                 String.Empty))
                Return items
            EndFunction
            ' The type converter needs to be generated with a standard             ' collection of items from the project.PrivateClass FileListTypeConverter
                Inherits TypeConverter

                PublicOverridesFunction GetStandardValues(ByVal context _
                  As ITypeDescriptorContext) As StandardValuesCollection

                    Dim myList As MyList = CType(context.Instance, MyList)
                    Dim webApp As IWebApplication = CType(myList._parent. _
                      Component.Site.GetService(GetType(IWebApplication)), _
                                                IWebApplication)

                    Dim xmlFiles AsNew ArrayList()
                    Dim rootItem As IFolderProjectItem = _
                      CType(webApp.RootProjectItem, IFolderProjectItem)
                    Dim item As IProjectItem
                    ForEach item In rootItem.Children
                        IfString.Equals( _
                          Path.GetExtension(item.Name), ".xml", _
                          StringComparison.CurrentCultureIgnoreCase) Then

                            xmlFiles.Add(item.Name)
                        EndIfNext item

                    ReturnNew StandardValuesCollection(xmlFiles)
                EndFunctionPublicOverridesFunction GetStandardValuesExclusive( _
                  ByVal context As ITypeDescriptorContext) AsBooleanReturnFalseEndFunctionPublicOverridesFunction GetStandardValuesSupported( _
                  ByVal context As ITypeDescriptorContext) AsBooleanReturnTrueEndFunctionEndClassEndClassEndClass
    ' This control designer is used to obtain the     ' configuration setting for the title.PublicClass ControlWithConfigurationSettingDesigner
        Inherits PanelContainerDesigner

        PublicSubNew()
        EndSubPrivate _style As Style = NothingPublicOverridesReadOnlyProperty FrameCaption() _
          AsStringGetDim title AsString = String.Empty
                Dim webApp As IWebApplication
                webApp = CType(Component.Site.GetService( _
                                  GetType(IWebApplication)), IWebApplication)
                IfNot (webApp IsNothing) Then                    ' Get the Configuration API.Dim config As System.Configuration.Configuration
                    config = webApp.OpenWebConfiguration(True)

                    IfNot (config IsNothing) ThenDim settingsSection As AppSettingsSection
                        settingsSection = config.AppSettings

                        IfNot (settingsSection IsNothing) Then
                            title = " .... " & _
                                settingsSection.Settings("ContainerControlTitle").ToString() _
                                & " ..... "EndIfEndIfEndIfReturn title
            EndGetEndPropertyPublicOverridesReadOnlyProperty FrameStyle() As Style
            GetIf _style IsNothingThen
                    _style = New Style()
                    _style.Font.Name = "Verdana"
                    _style.Font.Size = New FontUnit("XSmall")
                    _style.BackColor = Color.LightGreen
                    _style.ForeColor = Color.Black
                EndIfReturn _style
            EndGetEndPropertyEndClass
    ' This control designer reads from the project file.
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    PublicClass ControlWithButtonTasksDesigner
        Inherits PanelContainerDesigner

        PublicSubNew()
        EndSubPrivate _style As Style = Nothing
        ' Add the caption by default. Note that the caption         ' will only appear if the Web server control         ' allows child controls rather than properties. PublicOverridesReadOnlyProperty FrameCaption() _
          AsStringGetReturn"Container adding controls to the document."EndGetEndPropertyPublicOverridesReadOnlyProperty FrameStyle() _
          As Style

            GetIf _style IsNothingThen
                    _style = New Style()
                    _style.Font.Name = "Verdana"
                    _style.Font.Size = New FontUnit("XSmall")
                    _style.BackColor = Color.LightGreen
                    _style.ForeColor = Color.Black
                EndIfReturn _style
            EndGetEndProperty
        ' The following section creates a designer action list.        ' Add the specific action list for this control. The         ' procedure here depends upon the specific order for the         ' list to be added, but always add the base.PublicOverridesReadOnlyProperty ActionLists() _
          As DesignerActionListCollection

            GetDim newActionLists AsNew DesignerActionListCollection()
                newActionLists.AddRange(MyBase.ActionLists)
                newActionLists.Add(New ControlWithButtonTasksList(Me))
                Return newActionLists
            EndGetEndPropertyPublicSub AddButton()
            ' Add a standard button.Dim b AsNew Button()
            b.Text = "New Button"
            RootDesigner.AddControlToDocument(b, Nothing, ControlLocation.Last)
        EndSub 'AddButton

        PublicSub AddNewButton()
            ' Add your custom button.Dim b AsNew NewButton()
            b.Text = "New custom button"
            ' For buttons defined in a different assembly, add the              ' register directive for the referenced assembly.             ' By default, this goes to the document, unless             ' already defined in the document or in configuration.Dim wfrm As WebFormsReferenceManager = RootDesigner.ReferenceManager
            wfrm.RegisterTagPrefix(b.GetType())

            RootDesigner.AddControlToDocument(b, Nothing, ControlLocation.First)
        EndSub

        ' Create the action list for this control.PrivateClass ControlWithButtonTasksList
            Inherits DesignerActionList
            Private _parent As ControlWithButtonTasksDesigner

            PublicSubNew(ByVal parent As ControlWithButtonTasksDesigner)
                MyBase.New(parent.Component)
                _parent = parent
            EndSubPublicSub AddButton()
                _parent.AddButton()
            EndSubPublicSub AddNewButton()
                _parent.AddNewButton()
            EndSub
            ' Provide the list of sorted action items for the host.PublicOverridesFunction GetSortedActionItems() _
              As DesignerActionItemCollection

                Dim items AsNew DesignerActionItemCollection()

                items.Add(New DesignerActionTextItem("Add Control", "Add"))
                items.Add(New DesignerActionMethodItem(Me, _
                                               "AddButton", _
                                               "Add a Button", _
                                               "Add", _
                                               String.Empty, _
                                               False))
                items.Add(New DesignerActionMethodItem(Me, _
                                               "AddNewButton", _
                                               "Add a custom Button", _
                                               "Add", _
                                               String.Empty, _
                                               False))

                Return items
            EndFunctionEndClassEndClassEndNamespace
namespace Samples.AspNet.CS.Controls
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.UI.Design.WebControls;
    using System.Web.UI.WebControls;
    using System.Reflection;
    using System.Xml;
    using System.Configuration;

    // This region-based control renders the content of an XML file.
    [
      Designer(typeof(ControlWithStyleTasksDesigner)),
      ToolboxData("<{0}:ControlWithStyleTasks runat=\"server\" "
                  + "width=\"100%\"></{0}:ControlWithStyleTasks>")
    ]
    publicclass ControlWithStyleTasks : Panel { }

    [
      Designer(typeof(ControlWithConfigurationSettingDesigner)),
      ToolboxData("<{0}:ControlWithConfigurationSetting runat=\"server\" "
                  + "width=\"100%\"></{0}:ControlWithConfigurationSetting>")
    ]
    publicclass ControlWithConfigurationSetting : Panel { }

    [
      Designer(typeof(ControlWithButtonTasksDesigner)),
      ToolboxData("<{0}:ControlWithButtonTasks runat=\"server\" "
                  + "width=\"100%\"></{0}:ControlWithButtonTasks>")
    ]
    publicclass ControlWithButtonTasks : Panel { }


    // Declare a custom button to add dynamically at design time.publicclass NewButton : Button
    {
        public NewButton() { Text = "NewButton"; }
    }


    // This control designer is used to read xml files from the project.
    [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    publicclass ControlWithStyleTasksDesigner : PanelContainerDesigner
    {
        private Style _style = null;

        publicoverridestring FrameCaption
        {
            get
            {
                return"Container getting styles from the project item.";
            }
        }

        publicoverride Style FrameStyle
        {
            get
            {
                if (_style == null)
                {
                    _style = new Style();
                    _style.Font.Name = "Verdana";
                    _style.Font.Size = new FontUnit("XSmall");
                    _style.BackColor = Color.LightGreen;
                    _style.ForeColor = Color.Black;
                }

                return _style;
            }
        }

        // Create a convenience field for the control.private ControlWithStyleTasks myControl;
        publicoverridevoid Initialize(IComponent component)
        {
            // Create the convenience control.base.Initialize(component);
            myControl = (ControlWithStyleTasks)component;
        }

        // The following section creates a designer action list.// Add the specific action list for this control. The // procedure here depends upon the specific order for // the list to be added, but always add the base.publicoverride DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection actionLists =
                  new DesignerActionListCollection();
                actionLists.AddRange(base.ActionLists);
                actionLists.Add(new MyList(this));
                return actionLists;
            }
        }

        privatestring _styleConfigPhysicalFile = "";
        protectedstring StyleConfigurationFile
        {
            get { return _styleConfigPhysicalFile; }
            set
            {
                string styleConfigPhysicalFilePath = String.Empty;
                if (value.Length > 0)
                {
                    // Access the folder and look for the Control.xml file; // then obtain its physical path to use to set styles // for this control and control designer.  Obtain the // actual file and its physical path from WebApplication.
                    IWebApplication webApp =
                      (IWebApplication)Component.Site.GetService(
                        typeof(IWebApplication));
                    if (webApp != null)
                    {
                        // Look for the project items from the root.
                        IProjectItem dataFileProjectItem =
                          webApp.GetProjectItemFromUrl("~/" + value);
                        if (dataFileProjectItem != null)
                        {
                            _styleConfigPhysicalFile = value;
                            styleConfigPhysicalFilePath =
                              dataFileProjectItem.PhysicalPath;
                        }
                    }
                }

                // Get the styles from the XML file.
                SetControlStyleFromConfiguration(styleConfigPhysicalFilePath);
            }
        }

        // Open the XML document and set control properties directly.privatevoid SetControlStyleFromConfiguration(string path)
        {
            if (path == null || path == String.Empty)
                return;

            WebControl wc = (WebControl)Component;
            if (wc == null)
                return;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);

            XmlNodeList stylesList = xmlDoc.GetElementsByTagName("style");
            for (int i = 0; i < stylesList.Count; i+)
            {
                if (stylesList[i].Attributes["name"].Value == "BackColor")
                {
                    PropertyDescriptor pd =
                      TypeDescriptor.GetProperties(wc)["BackColor"];
                    if (pd != null)
                        pd.SetValue(wc,
                          Color.FromName(stylesList[i].Attributes["value"].Value));
                }
                if (stylesList[i].Attributes["name"].Value == "ForeColor")
                {
                    PropertyDescriptor pd =
                      TypeDescriptor.GetProperties(wc)["ForeColor"];
                    if (pd != null)
                        pd.SetValue(wc,
                          Color.FromName(stylesList[i].Attributes["value"].Value));
                }
            }
        }

        // Create the action list for this control.privateclass MyList : DesignerActionList
        {
            private ControlWithStyleTasksDesigner _parent;

            public MyList(ControlWithStyleTasksDesigner parent)
                : base(parent.Component)
            {
                _parent = parent;
            }

            [
              TypeConverter(typeof(FileListTypeConverter))
            ]
            publicstring ConfigureControlStyle
            {
                get { return _parent.StyleConfigurationFile; }
                set
                {
                    _parent.StyleConfigurationFile = value;
                }
            }

            // Provide the list of sorted action items for the host.// Note that you can define the items through constructors // or through metadata on the method or property items.publicoverride DesignerActionItemCollection GetSortedActionItems()
            {
                DesignerActionItemCollection items = new DesignerActionItemCollection();

                items.Add(new DesignerActionTextItem("Configuration",
                                                      "Select"));
                items.Add(new DesignerActionPropertyItem("ConfigureControlStyle",
                                               "Configure XML",
                                               "Select",
                                               String.Empty));
                return items;
            }

            // The type converter needs to be generated with a standard // collection of items from the project.privateclass FileListTypeConverter : TypeConverter
            {
                publicoverride StandardValuesCollection
                  GetStandardValues(ITypeDescriptorContext context)
                {
                    MyList myList = (MyList)context.Instance;
                    IWebApplication webApp =
                      (IWebApplication)myList._parent.Component.Site.GetService(
                        typeof(IWebApplication));

                    ArrayList xmlFiles = new ArrayList();
                    IFolderProjectItem rootItem =
                      (IFolderProjectItem)webApp.RootProjectItem;
                    foreach (IProjectItem item in rootItem.Children)
                    {
                        if (String.Equals(Path.GetExtension(item.Name),
                                          ".xml",
                                          StringComparison.CurrentCultureIgnoreCase))
                        {
                            xmlFiles.Add(item.Name);
                        }
                    }

                    returnnew StandardValuesCollection(xmlFiles);
                }

                publicoverridebool GetStandardValuesExclusive(
                  ITypeDescriptorContext context)
                {
                    returnfalse;
                }
                publicoverridebool GetStandardValuesSupported(
                  ITypeDescriptorContext context)
                {
                    returntrue;
                }
            }
        }

    }

    // This control designer is used to obtain the // configuration setting for the title.publicclass ControlWithConfigurationSettingDesigner : PanelContainerDesigner
    {
        public ControlWithConfigurationSettingDesigner() { }

        private Style _style = null;

        publicoverridestring FrameCaption
        {
            get
            {
                string title = String.Empty;
                IWebApplication webApp =
                  (IWebApplication)Component.Site.GetService(
                    typeof(IWebApplication));
                if (webApp != null)
                {
                    // Get the Configuration API.
                    Configuration config = webApp.OpenWebConfiguration(true);
                    if (config != null)
                    {
                        AppSettingsSection settingsSection = config.AppSettings;
                        if (settingsSection != null)
                        {
                            title =
                              " .... "
                              + settingsSection.Settings["ContainerControlTitle"]
                              + " ..... ";
                        }
                    }
                }
                return title;
            }
        }

        publicoverride Style FrameStyle
        {
            get
            {
                if (_style == null)
                {
                    _style = new Style();
                    _style.Font.Name = "Verdana";
                    _style.Font.Size = new FontUnit("XSmall");
                    _style.BackColor = Color.LightGreen;
                    _style.ForeColor = Color.Black;
                }

                return _style;
            }
        }
    }

    // This control designer reads from the project file.
    [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    publicclass ControlWithButtonTasksDesigner : PanelContainerDesigner
    {
        public ControlWithButtonTasksDesigner() { }

        private Style _style = null;

        // Add the caption by default. Note that the caption // will only appear if the Web server control // allows child controls rather than properties. publicoverridestring FrameCaption
        {
            get { return"Container adding controls to the document."; }
        }

        publicoverride Style FrameStyle
        {
            get
            {
                if (_style == null)
                {
                    _style = new Style();
                    _style.Font.Name = "Verdana";
                    _style.Font.Size = new FontUnit("XSmall");
                    _style.BackColor = Color.LightGreen;
                    _style.ForeColor = Color.Black;
                }

                return _style;
            }
        }

        // The following section creates a designer action list.// Add the specific action list for this control. The // procedure here depends upon the specific order for the // list to be added, but always add the base.publicoverride DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection actionLists =
                  new DesignerActionListCollection();
                actionLists.AddRange(base.ActionLists);
                actionLists.Add(new MyList3(this));
                return actionLists;
            }
        }

        publicvoid AddButton()
        {
            // Add a standard button.
            Button b = new Button();
            b.Text = "New standard button";
            RootDesigner.AddControlToDocument(b, null, ControlLocation.First);
        }


        publicvoid AddNewButton()
        {
            // Add your custom button.
            NewButton b = new NewButton();
            b.Text = "New custom button";

            // For buttons defined in a different assembly, add the  // register directive for the referenced assembly. // assembly. By default, this goes to the document, unless // already defined in the document or in configuration.
            WebFormsReferenceManager wfrm =
              RootDesigner.ReferenceManager;
            wfrm.RegisterTagPrefix(b.GetType());

            RootDesigner.AddControlToDocument(b, null, ControlLocation.Last);
        }


        // Create the action list for this control.privateclass MyList3 : DesignerActionList
        {
            private ControlWithButtonTasksDesigner _parent;
            public MyList3(ControlWithButtonTasksDesigner parent)
                : base(parent.Component)
            {
                _parent = parent;
            }

            publicvoid AddButton()
            {
                _parent.AddButton();
            }
            publicvoid AddNewButton()
            {
                _parent.AddNewButton();
            }

            // Provide the list of sorted action items for the host.publicoverride DesignerActionItemCollection GetSortedActionItems()
            {
                DesignerActionItemCollection items = new DesignerActionItemCollection();

                items.Add(new DesignerActionTextItem("Add Control", "Add"));
                items.Add(new DesignerActionMethodItem(this,
                                               "AddButton",
                                               "Add a Button",
                                               "Add",
                                               String.Empty,
                                               false));
                items.Add(new DesignerActionMethodItem(this,
                                               "AddNewButton",
                                               "Add a custom Button",
                                               "Add",
                                               String.Empty,
                                               false));
                return items;
            }
        }
    }
}

See Also

Tasks

Walkthrough: Creating a Basic Control Designer for a Web Server Control

Concepts

ASP.NET Control Designers Overview

Sample Control Designer with Action Lists and Services