Export (0) Print
Expand All

ControlDesigner Class

Provides a base designer class for extending the design-time behavior of a Web server control.

For a list of all members of this type, see ControlDesigner Members.

System.Object
   System.ComponentModel.Design.ComponentDesigner
      System.Web.UI.Design.HtmlControlDesigner
         System.Web.UI.Design.ControlDesigner
            Derived classes

[Visual Basic]
Public Class ControlDesigner
   Inherits HtmlControlDesigner
[C#]
public class ControlDesigner : HtmlControlDesigner
[C++]
public __gc class ControlDesigner : public HtmlControlDesigner
[JScript]
public class ControlDesigner extends HtmlControlDesigner

Thread Safety

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

Remarks

ControlDesigner provides a base designer class that can be inherited from and extended to provide design-time support for a custom Web Forms control. ControlDesigner also provides the following methods that a designer can call at design time for a variety of purposes:

  • AllowResize indicates whether the control can be resized.
  • DesignTimeHtmlRequiresLoadComplete indicates whether the HTML of the control can be displayed before loading has completed.
  • IsDirty indicates whether the control has been changed since it was loaded or last saved.
  • IsPropertyBound indicates whether a specified property is data-bound.
  • ReadOnly indicates whether the control is read-only and cannot be modified at design-time.
  • UpdateDesignTimeHtml updates the display of the control, typically after values have been changed by a designer.

Example

[Visual Basic, C#, C++] The following code example creates a designer class that derives from the ControlDesigner class. This designer provides a command that changes the text size of a control at design time. The class that the designer supports is named TextControl. The control is associated with the designer by specifying the designer in a DesignerAttribute meta tag declaration on the TextControl class.

[Visual Basic] 
Imports System
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports Microsoft.VisualBasic

Namespace Examples.AspNet

' This control designer offers designer verb menu commands
' that can alter the design time html provided for the 
' System.Web.UI.Control this designer supports.
Public Class TextSizeWebControlDesigner
    Inherits System.Web.UI.Design.ControlDesigner
    ' Whether to display the html of the associated 
    ' control using a large heading text size.
    Private LargeText As Boolean
    Private dvc As DesignerVerbCollection = Nothing

    Public Sub New()
        LargeText = True
    End Sub

    ' Provides a menu command to toggle the text size.
    Public Overrides ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection
        Get
            Dim verbReduce As DesignerVerb = _
              New DesignerVerb("Reduce text size", New EventHandler(AddressOf Me.toggleTextSize))
            Dim verbEnlarge As DesignerVerb = _
              New DesignerVerb("Enlarge text size", New EventHandler(AddressOf Me.toggleTextSize))
             
            If dvc Is Nothing Then
                dvc = New DesignerVerbCollection()
                dvc.Add(verbReduce)
                dvc.Add(verbEnlarge)
            ElseIf (dvc.Contains(verbEnlarge) = False) Then
                dvc.Add(verbEnlarge)
            ElseIf (dvc.Contains(verbReduce) = False) Then
                dvc.Add(verbReduce)
            End If
 
            If LargeText Then
                dvc.Remove(verbEnlarge)
            Else
                dvc.Remove(verbReduce)
            End If
            Return dvc
        End Get
    End Property

    ' Returns the html to use to represent the control at design time.
    Public Overrides Function GetDesignTimeHtml() As String
        Dim html As String = MyBase.GetDesignTimeHtml()
        

        If LargeText Then
            Return "<H1>" + html + "</H1>"
        Else
            Return "<H3>" + html + "</H3>"
        End If
    End Function

    ' Event handler to toggle whether the html receives a large or 
    ' small size heading markup tag.
    Private Sub ToggleTextSize(ByVal sender As Object, ByVal e As EventArgs)
        If LargeText Then
            LargeText = False
        Else
            LargeText = True
        End If
        Me.IsDirty = True
        Me.UpdateDesignTimeHtml()
    End Sub

End Class

' Simple text Web control renders a text string.
' This control is associated with the TextSizeWebControlDesigner.
<DesignerAttribute(GetType(TextSizeWebControlDesigner), GetType(IDesigner))> _
Public Class TextControl
    Inherits System.Web.UI.WebControls.WebControl
    Private [text_] As String

    <Bindable(True), Category("Appearance"), DefaultValue("")> _
    Public Property [Text]() As String
      Get
         Dim o As Object = ViewState("Text")
         Return IIf(o Is Nothing, String.Empty, CStr(o))
      End Get
      Set
         If HasControls() Then
            Controls.Clear()
         End If
         ViewState("Text") = value
      End Set
    End Property

    Public Sub New()
        [text_] = "Test phrase"
    End Sub

    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
        output.Write([Text])
    End Sub

End Class

[C#] 
using System;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace Examples.AspNet
{    
    // This control designer offers designer verb menu commands
    // that can alter the design time html provided for the 
    // System.Web.UI.Control this designer supports.
    public class TextSizeWebControlDesigner : System.Web.UI.Design.ControlDesigner
    {
        // Whether to display the html of the associated 
        // control using a large heading text size.
        private bool LargeText;
        private DesignerVerbCollection dvc;        

        public TextSizeWebControlDesigner() : base()
        {
            LargeText = true;                  
        }

        // Provides a menu command to toggle the text size.
        public override System.ComponentModel.Design.DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerb verbReduce = new DesignerVerb("Reduce text size", new EventHandler(this.ToggleTextSize));
                DesignerVerb verbEnlarge = new DesignerVerb("Enlarge text size", new EventHandler(this.ToggleTextSize));
                
                if (dvc == null){     
                    dvc = new DesignerVerbCollection();
                    dvc.Add(verbReduce);
                    dvc.Add(verbEnlarge);
                }        
                else if (dvc.Contains(verbEnlarge) == false){
                    dvc.Add(verbEnlarge);
                }
                else if (dvc.Contains(verbReduce) == false){
                    dvc.Add(verbReduce);
                }                   
                
                if( LargeText )
                    dvc.Remove(verbEnlarge);
                else
                    dvc.Remove(verbReduce);
                return dvc;
            }
        }

        // Returns the html to use to represent the control at design time.
        public override string GetDesignTimeHtml()
        {
            string html = base.GetDesignTimeHtml();

            if( LargeText )
                return "<H1>"+html+"</H1>";
            else
                return "<H3>"+html+"</H3>";                        
        }        

        // Event handler to toggle whether the html receives a large or 
        // small size heading markup tag.
        private void ToggleTextSize(object sender, EventArgs e)
        {
            if( LargeText )
                LargeText = false;
            else
                LargeText = true;
            this.IsDirty = true;
            this.UpdateDesignTimeHtml();
        }        
    }

    // Simple text Web control renders a text string.
    // This control is associated with the TextSizeWebControlDesigner.
    [DesignerAttribute(typeof(TextSizeWebControlDesigner), typeof(IDesigner))]
    public class TextControl : System.Web.UI.WebControls.WebControl
    {

        [Bindable(true),
            Category("Appearance"),
            DefaultValue("")]
        public string Text
        {
                    get
                    {
                        object o = ViewState["Text"];
                        return((o == null) ? String.Empty : (string)o);
                    }

                    set
                    {
                        if (HasControls()) {
                            Controls.Clear();
                        }
                        ViewState["Text"] = value;
                    }
        }

        public TextControl()
        {
            Text = "Test phrase";
        }

    protected override void RenderContents(HtmlTextWriter output)
    {
        output.Write(Text);
    }
    }    
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
#using <System.Design.dll>
using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::Design;
using namespace System::Web::UI::WebControls;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;

// This control designer offers designer verb menu commands
// that can alter the design time html provided for the
// System::Web::UI::Control this designer supports.
public __gc class TextSizeWebControlDesigner : public ControlDesigner {
   // Whether to display the html of the associated
   // control using a large heading text size.
private:
   bool LargeText;

public:
   TextSizeWebControlDesigner() : ControlDesigner() {
      LargeText = true;
   }

   // Provides a menu command to toggle the text size.
public:
   __property System::ComponentModel::Design::DesignerVerbCollection* get_Verbs() {
      DesignerVerbCollection* dvc = new DesignerVerbCollection();
      if (LargeText)
         dvc->Add(new DesignerVerb(S"Reduce text size", 
         new EventHandler(this, &TextSizeWebControlDesigner::ToggleTextSize)));
      else
         dvc->Add(new DesignerVerb(S"Enlarge text size", 
         new EventHandler(this, &TextSizeWebControlDesigner::ToggleTextSize)));
      return dvc;
   }


   // Returns the html to use to represent the control at design time.
public:
   String* GetDesignTimeHtml() {
      String* html = __super::GetDesignTimeHtml();

      if (LargeText)
         return String::Concat(S"<H1> ", html, S"</H1>");
      else
         return String::Concat(S"<H3> ", html, S"</H3>");
   }

   // Event handler to toggle whether the html receives a large or
   // small size heading markup tag.
private:
   void ToggleTextSize(Object* /*sender*/, EventArgs* /*e*/) {
      if (LargeText)
         LargeText = false;
      else
         LargeText = true;
      this->IsDirty = true;
      this->UpdateDesignTimeHtml();
   }
};

// Simple text Web control renders a text string.
// This control is associated with the TextSizeWebControlDesigner.
[DesignerAttribute(__typeof(TextSizeWebControlDesigner), __typeof(IDesigner))]
public __gc class TextControl : public WebControl {
private:
   String* text;

public:
   [Bindable(true),
      Category(S"Appearance"),
      DefaultValue(S"")]
__property String* get_Text() {
         return text;
      }
__property void set_Text(String* value) {
         text = value;
      }


   TextControl() {
      text = S"Test phrase";
   }

protected:
   void Render(HtmlTextWriter* output) {
      output->Write(Text);
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.Design

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Design (in System.Design.dll)

See Also

ControlDesigner Members | System.Web.UI.Design Namespace | HtmlControlDesigner

Show:
© 2014 Microsoft