Export (0) Print
Expand All

WebPartDesigner Class

Provides design-time visual support for WebPart controls.

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

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class WebPartDesigner : PartDesigner

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
{
  [AspNetHostingPermission(SecurityAction.Demand, 
    Level=AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand, 
    Level=AspNetHostingPermissionLevel.Minimal)]
  [Designer(typeof(BirthdayPartDesigner))]
  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),
      WebBrowsable()
    ]
    public DateTime BirthDate
    {
      get { return birthDate; }
      set { birthDate = value; }
    }

    // Set the appearance of the control at run time. 
    protected override void CreateChildControls()
    {
      Controls.Clear();
      input = new Calendar();
      this.Controls.Add(input);
      Button update = new Button();
      update.Text = "Submit";
      update.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(update);
      displayContent = new Label();
      displayContent.BackColor = 
      System.Drawing.Color.LightBlue;
      Literal br = new Literal();
      br.Text = "<br />";
      if ((this.birthDate.Day == DateTime.Now.Day)
        && (this.birthDate.Month == DateTime.Now.Month))
      {
        displayContent.Text = "Happy Birthday!";
        this.Controls.Add(br);
        this.Controls.Add(displayContent);
      }
    }

    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");
      }
      base.Initialize(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;
      try
      {
        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);
      }
      finally
      {
        // 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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2015 Microsoft