This documentation is archived and is not being maintained.

WebPartDesigner Class

Provides design-time visual support for WebPart controls.

Namespace: System.Web.UI.Design.WebControls.WebParts
Assembly: System.Design (in

public class WebPartDesigner : PartDesigner
public class WebPartDesigner extends PartDesigner
public class WebPartDesigner extends PartDesigner
Not applicable.

The WebPartDesigner class provides the visual representation for WebPart controls at design time. It is derived from the PartDesigner class and adds validation to verify that the associated control is a WebPart control or derived type.

WebPartDesigner inherits a UsePreviewControl property from its parent that is always set to true. This causes the visual design environment to generate a View control to hold a temporary copy of the WebPart on the design surface; this copy is then persisted into markup. If you override the UsePreviewControl property to return false, the visual design environment generates markup directly from the actual WebPart control.

Web Parts designers generally act much like regular control designers; the principal methods for setting design-time appearance derive from the ControlDesigner class. Specifically, you can override the GetDesignTimeHtml method to change the design-time markup associated with the control. You can likewise override the GetErrorDesignTimeHtml and GetEmptyDesignTimeHtml methods to handle errors and empty strings (""), respectively.

The following code example shows the interaction between a WebPart control and its associated WebPartDesigner. The WebPart control contains a Calendar control from which the end user selects his or her birthday, a Button control to submit the selection, and a Label control to display a message on the user's birthday. The WebPartDesigner verifies that the associated control is of the expected type and then customizes the design-time rendering of said control. Note that the visual customizations of the designer are visible only at design time, whereas those of the associated control are visible at both run time and design time.

All the methods overridden in this example derive from the ControlDesigner base class. For other available members and their use, see System.Web.UI.Design.ControlDesigner.

using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.Design.WebControls.WebParts;
using System.ComponentModel;

/// <summary>
/// BirthdayPart demonstrates some of the most
/// common overrides of members of the WebPart
/// class. BirthdayPartDesigner shows how to 
/// customize the rendering of a custom WebPart
/// control.
/// </summary>
namespace Samples.AspNet.CS.Controls
  public class BirthdayPart : WebPart
    private DateTime birthDate;
    Calendar input;
    Label displayContent;

    public BirthdayPart()
      this.AllowClose = false;
      this.Title = "Enter your birthday";

      Personalizable(PersonalizationScope.User, true),
    public DateTime BirthDate
      get { return birthDate; }
      set { birthDate = value; }
    // Set the appearance of the control at run time.
    protected override void CreateChildControls()
      input = new Calendar();
      Button update = new Button();
      update.Text = "Submit";
      update.Click += new EventHandler(this.submit_Click);
      displayContent = new Label();
      displayContent.BackColor = 
      Literal br = new Literal();
      br.Text = "<br />";
      if ((this.birthDate.Day == DateTime.Now.Day)
        && (this.birthDate.Month == DateTime.Now.Month))
        displayContent.Text = "Happy Birthday!";

    private void submit_Click(object sender, EventArgs e)
      this.birthDate = input.SelectedDate;

  public class BirthdayPartDesigner : WebPartDesigner
    public override void Initialize(IComponent component)
      // Verify that the associated control is a BirthdayPart.
      if (!typeof(BirthdayPart).IsInstanceOfType(component))
        throw new ArgumentException("The specified control is not of type 'BirthdayPart'", "component");

    // Here is where you make customizations
    // to design time appearance that will not
    // be visible to the end user.
    public override string GetDesignTimeHtml()
      string designTimeHtml = null;
        designTimeHtml = base.GetDesignTimeHtml();
        string s = "<hr /><hr />I just added these lines to the"
          + " bottom of the control.<hr /><hr /></div>";
        designTimeHtml = designTimeHtml.Replace("</div>", s);
      catch (Exception ex)
        designTimeHtml = GetErrorDesignTimeHtml(ex);
        // undo any changes in the try block

      if ((designTimeHtml == null) || (designTimeHtml.Length == 0))
        designTimeHtml = GetEmptyDesignTimeHtml();
      return designTimeHtml;

    // This method normally returns a blank string.
    // Override to return a meaningful message.
    protected override string GetEmptyDesignTimeHtml()
      return CreatePlaceHolderDesignTimeHtml(
        "<hr />If the page developer forgot to fill in a " +
        "required property you could tell them here.<hr />");
    // Add specific text to the generic error message.
    protected override string GetErrorDesignTimeHtml(Exception e)
      string s = "<hr />The control failed to render.";
      return(s + e.Message + "<hr />");

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0