Export (0) Print
Expand All
2 out of 7 rated this helpful - Rate this topic

EditorPart Class

Note: This class is new in the .NET Framework version 2.0.

Serves as the base class for controls that reside in EditorZoneBase zones and are used to edit WebPart controls.

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

[BindableAttribute(false)] 
public abstract class EditorPart : Part
/** @attribute BindableAttribute(false) */ 
public abstract class EditorPart extends Part
BindableAttribute(false) 
public abstract class EditorPart extends Part

The EditorPart class provides a base set of properties and methods that are used by the derived EditorPart controls provided with the Web Parts control set, and by custom EditorPart controls. An EditorPart control allows a user to edit an associated WebPart control by modifying its layout, appearance, properties, behavior, or other characteristics.

The editing user interface (UI), which consists of EditorZoneBase zones that contain EditorPart controls, is displayed after a Web page enters edit mode.

NoteNote

An EditorPart control can be used only within a zone that derives from the EditorZoneBase class, such as the EditorZone control.

After a page has entered edit mode, a user can select a WebPart control to edit by clicking an edit verb on the verbs menu of the selected control. Only one WebPart control can be edited at a time.

Several derived EditorPart controls are provided with the Web Parts control set, as listed in the following table. These controls provide all the editing capabilities that are necessary for most applications. The typical approach for using these controls in a Web Parts page is to declare them in page persistence format within a <zonetemplate> element, where <zonetemplate> is the child element of an <asp:editorzone> element. For a code example, see the Example section of this topic.

Type of control

Description

AppearanceEditorPart

Edits the appearance of the associated control, including properties such as its title text, height, width, and border attributes.

BehaviorEditorPart

Edits certain behaviors of the associated control, such as whether it can be edited, whether it can be closed, or whether it can be moved to another zone. This control is only visible on a page when a control is being edited in shared personalization scope.

LayoutEditorPart

Edits layout attributes for the associated control, such as whether it is in a normal or minimized (collapsed) state, and what zone it is placed in.

PropertyGridEditorPart

Edits properties of the associated control, if those properties were declared in the source code with a WebBrowsable attribute.

NoteNote

To improve accessibility, all the EditorPart controls provided in the Web Parts control set are rendered within a <fieldset> element. The <fieldset> element groups the related set of controls used for editing in a given EditorPart control, and it facilitates tabbed navigation among those controls for both visual user agents (such as ordinary Web browsers) and speech-oriented user agents (such as screen-reading software).

Notes to Inheritors To create a custom EditorPart control, you must override two important methods. The ApplyChanges method applies changes made in the editor control to the WebPart control being edited. The SyncChanges method gets the current values of the WebPart control being edited, so that the editor control can edit them. Using these two critical methods, you get and set values between your custom EditorPart control and the WebPart control it is editing. If you derive from the EditorPart class to create custom editor controls, you can add your custom controls to an EditorZoneBase zone by implementing the IWebEditable interface in a WebPart control, a user control, or a server control. For more details and a code example, see the IWebEditable class overview. Also, the code example in the Example section demonstrates how to implement IWebEditable in a custom WebPart control.

TopicLocation
How to: Set the Display Mode of a Web Parts PageBuilding ASP .NET Web Applications
How to: Set the Display Mode of a Web Parts PageBuilding ASP .NET Web Applications

The following code example demonstrates declarative and programmatic use of EditorPart controls. This code example has four parts:

  • A user control that enables you to change display modes on a Web Parts page.

  • A Web page that contains an EditorZone control, with several of the EditorPart controls from the Web Parts control set declared in the zone, and a reference to a custom WebPart control.

  • A class that contains the custom WebPart control, and a custom EditorPart control for editing a property in the WebPart control.

  • An explanation of how the example works when you load the page in a browser.

The first part of this code example is the user control that enables users to change display modes on a Web page. For details about display modes and a description of the source code in this control, see Walkthrough: Changing Display Modes on a Web Parts Page.

<%@ control language="C#" classname="DisplayModeMenuCS"%>

<script runat="server">
  
 // Use a field to reference the current WebPartManager.
  WebPartManager _manager;

  void Page_Init(object sender, EventArgs e)
  {
    Page.InitComplete += new EventHandler(InitComplete);
  }  

  void InitComplete(object sender, System.EventArgs e)
  {
    _manager = WebPartManager.GetCurrentWebPartManager(Page);

    String browseModeName = WebPartManager.BrowseDisplayMode.Name;

    // Fill the dropdown with the names of supported display modes.
    foreach (WebPartDisplayMode mode in _manager.SupportedDisplayModes)
    {
      String modeName = mode.Name;
      // Make sure a mode is enabled before adding it.
      if (mode.IsEnabled(_manager))
      {
        ListItem item = new ListItem(modeName + " Mode", modeName);
        DisplayModeDropdown.Items.Add(item);
      }
    }

  }
 
  // Change the page to the selected display mode.
  void DisplayModeDropdown_SelectedIndexChanged(object sender, 
    EventArgs e)
  {
    String selectedMode = DisplayModeDropdown.SelectedValue;

    WebPartDisplayMode mode = _manager.SupportedDisplayModes[selectedMode];
    if (mode != null)
      _manager.DisplayMode = mode;

  }

  void Page_PreRender(object sender, EventArgs e)
  {
    DisplayModeDropdown.SelectedValue = _manager.DisplayMode.Name;
  }
  
</script>
<div>
  <asp:DropDownList ID="DisplayModeDropdown" 
    runat="server"  
    AutoPostBack="true" 
    OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
</div>

The second part of the code example is the Web page. It contains a declarative reference to an EditorZone control, with a child <zonetemplate> element that contains declarative references to two of the Web Parts control set EditorPart controls. The page also references a custom WebPart control, using a Register directive for the assembly, and the <aspSample:TextDisplayWebPart> element for the control.

<%@ page language="c#" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenu" 
  Src="DisplayModecs.ascx" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.CS.Controls" 
  Assembly="TextDisplayWebPartCS" %>

<html>
  <head runat="server">
    <title>
      Text Display WebPart with EditorPart
    </title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:webpartmanager id="WebPartManager1" runat="server" />
      <uc1:DisplayModeMenu ID="DisplayModeMenu1" runat="server" />
      <asp:webpartzone id="zone1" runat="server" 
        CloseVerb-Enabled="false">
        <zonetemplate>
          <aspSample:TextDisplayWebPart 
            runat="server"   
            id="textwebpart" 
            title = "Text Content WebPart" />          
        </zonetemplate>
      </asp:webpartzone> 
      <asp:EditorZone ID="EditorZone1" runat="server">
        <ZoneTemplate>
          <asp:AppearanceEditorPart ID="AppearanceEditorPart1" 
            runat="server" />
          <asp:LayoutEditorPart ID="LayoutEditorPart1" 
            runat="server" />
        </ZoneTemplate>      
      </asp:EditorZone>
    </form>
  </body>
</html>

The third part of the code example is a custom WebPart class named TextDisplayWebPart. The class implements the IWebEditable interface. Within this class is a nested, private class that contains the code for a private EditorPart class associated with the TextDisplayWebPart class and named TextDisplayEditorPart. At run time, as the TextDisplayWebPart control enters edit mode, in its TextDisplayWebPart.CreateEditorParts method, it creates an instance of the TextDisplayEditorPart class, and displays it in the EditorZone control, along with the other EditorPart controls.

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class TextDisplayWebPart : WebPart
  {
    private String _contentText = null;
    private String _fontStyle = null;
    TextBox input;
    Label DisplayContent;
    Literal lineBreak;

    public override EditorPartCollection CreateEditorParts()
    {
      ArrayList editorArray = new ArrayList();
      TextDisplayEditorPart edPart = new TextDisplayEditorPart();
      edPart.ID = this.ID + "_editorPart1";
      editorArray.Add(edPart);
      EditorPartCollection editorParts = 
        new EditorPartCollection(editorArray);
      return editorParts;
    }

    public override object WebBrowsableObject
    {
      get { return this; }
    }

    [Personalizable(), WebBrowsable]
    public String ContentText
    {
      get { return _contentText; }
      set { _contentText = value; }
    }

    [Personalizable(), WebBrowsable()]
    public String FontStyle
    {
      get { return _fontStyle; }
      set { _fontStyle = value; }
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      DisplayContent.BackColor = Color.LightBlue;
      DisplayContent.Text = this.ContentText;
      if (FontStyle == null)
        FontStyle = "None";
      SetFontStyle(DisplayContent, FontStyle);
      this.Controls.Add(DisplayContent);

      lineBreak = new Literal();
      lineBreak.Text = @"<br />";
      Controls.Add(lineBreak);

      input = new TextBox();
      this.Controls.Add(input);
      Button update = new Button();
      update.Text = "Set Label Content";
      update.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(update);

    }

    private void submit_Click(object sender, EventArgs e)
    {
      // Update the label string.
      if (input.Text != String.Empty)
      {
        _contentText = input.Text + @"<br />";
        input.Text = String.Empty;
        DisplayContent.Text = this.ContentText;
      }
    }

    private void SetFontStyle(Label label, String selectedStyle)
    {
      if (selectedStyle == "Bold")
      {
        label.Font.Bold = true;
        label.Font.Italic = false;
        label.Font.Underline = false;
      }
      else if (selectedStyle == "Italic")
      {
        label.Font.Italic = true;
        label.Font.Bold = false;
        label.Font.Underline = false;
      }
      else if (selectedStyle == "Underline")
      {
        label.Font.Underline = true;
        label.Font.Bold = false;
        label.Font.Italic = false;
      }
      else
      {
        label.Font.Bold = false;
        label.Font.Italic = false;
        label.Font.Underline = false;
      }
    }

    // Create a custom EditorPart to edit the WebPart control.
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    private class TextDisplayEditorPart : EditorPart
    {
      DropDownList _partContentFontStyle;

      public TextDisplayEditorPart()
      {
        Title = "Font Face";
      }

      public override bool ApplyChanges()
      {
        TextDisplayWebPart part = 
          (TextDisplayWebPart)WebPartToEdit;
        // Update the custom WebPart control with the font style.
        part.FontStyle = PartContentFontStyle.SelectedValue;

        return true;
      }

      public override void SyncChanges()
      {
        TextDisplayWebPart part = 
          (TextDisplayWebPart)WebPartToEdit;
        String currentStyle = part.FontStyle;

        // Select the current font style in the drop-down control.
        foreach (ListItem item in PartContentFontStyle.Items)
        {
          if (item.Value == currentStyle)
          {
            item.Selected = true;
            break;
          }
        }
      }


      protected override void CreateChildControls()
      {
        Controls.Clear();

        // Add a set of font styles to the dropdown list.
        _partContentFontStyle = new DropDownList();
        _partContentFontStyle.Items.Add("Bold");
        _partContentFontStyle.Items.Add("Italic");
        _partContentFontStyle.Items.Add("Underline");
        _partContentFontStyle.Items.Add("None");

        Controls.Add(_partContentFontStyle);

      }

      protected override void RenderContents(HtmlTextWriter writer)
      {
        writer.Write("<b>Text Content Font Style</b>");
        writer.WriteBreak();
        writer.Write("Select a font style.");
        writer.WriteBreak();
        _partContentFontStyle.RenderControl(writer);
        writer.WriteBreak();
      }

      // Access the drop-down control through a property.
      private DropDownList PartContentFontStyle
      {
        get 
        {
          EnsureChildControls();
          return _partContentFontStyle;
        }
      }
    }
  }
}

Load the page in a browser, and on the Display Mode control, select Edit Mode to switch to edit mode. Click the verbs menu (the downward arrow) in the title bar of the TextDisplayWebPart control, and click Edit to edit the control. When the editing UI is visible, you can see three EditorPart controls, including the custom one that enables you to edit the TextDisplayWebPart.FontStyle property. If you make some changes in the editing UI and click the Apply button, you can use the drop-down list control to return the page to browse mode and see the full effect of the editing changes.

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.