EditorPartCollection Class
Assembly: System.Web (in system.web.dll)
The EditorPartCollection class is a read-only collection of EditorPart controls, typically used by an EditorZoneBase zone to track the set of EditorPart controls contained by the zone.
When a Web Parts page enters edit mode, and a user selects a control to edit, the editing process begins. The zone creates a new EditorPartCollection object consisting of the EditorPart controls contained by the zone. At various phases in the editing process, the zone accesses the EditorPartCollection object to save or retrieve property values between the EditorPart controls in the collection and the WebPart control currently being edited.
You can create an EditorPartCollection collection of controls for your own programmatic use if, for example, you need to perform some mass operation on a set of EditorPart controls. Even though the EditorPartCollection object is read-only, you can make programmatic changes to the properties of the underlying controls referenced in the collection.
The following code example demonstrates several uses of the EditorPartCollection class. There are four parts to this code example:
-
A user control that enables you to change display modes on a Web Parts page.
-
A class for a custom WebPart control named TextDisplayWebPart, which is referenced in the Web page and is edited by the EditorPart controls.
-
A Web page that references the TextDisplayWebPart control, contains an EditorZone control with several of the EditorPart controls from the Web Parts control set declared in the zone, and contains some event-driven code to create and manipulate an EditorPartCollection object.
-
An explanation of how the code example works when you load it 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="vb" classname="DisplayModeMenuVB"%> <script runat="server"> ' Use a field to reference the current WebPartManager. Dim _manager As WebPartManager Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) AddHandler Page.InitComplete, AddressOf InitComplete End Sub Sub InitComplete(ByVal sender As Object, ByVal e As System.EventArgs) _manager = WebPartManager.GetCurrentWebPartManager(Page) Dim browseModeName As String = _ WebPartManager.BrowseDisplayMode.Name ' Fill the dropdown with the names of supported display modes. Dim mode As WebPartDisplayMode For Each mode In _manager.SupportedDisplayModes Dim modeName As String = mode.Name ' Make sure a mode is enabled before adding it. If mode.IsEnabled(_manager) Then Dim item As New ListItem(modeName, modeName) DisplayModeDropdown.Items.Add(item) End If Next mode End Sub ' Change the page to the selected display mode. Sub DisplayModeDropdown_SelectedIndexChanged(ByVal sender As Object, _ ByVal e As EventArgs) Dim selectedMode As String = DisplayModeDropdown.SelectedValue Dim mode As WebPartDisplayMode = _ _manager.SupportedDisplayModes(selectedMode) If Not (mode Is Nothing) Then _manager.DisplayMode = mode End If End Sub Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) DisplayModeDropdown.SelectedValue = _manager.DisplayMode.Name End Sub </script> <div> <asp:Panel ID="Panel1" runat="server" Borderwidth="1" Width="125" BackColor="lightgray" Font-Names="Verdana, Arial, Sans Serif" > <asp:Label ID="Label1" runat="server" Text=" Display Mode" Font-Bold="true" Font-Size="8" Width="120" /> <asp:DropDownList ID="DisplayModeDropdown" runat="server" AutoPostBack="true" Width="120" OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" /> </asp:Panel> </div>
The second part of the code example is the TextDisplayWebPart control. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. For a walkthrough that demonstrates both methods of compiling, see Walkthrough: Developing and Using a Custom Server Control.
Note that the control has a property named ContentText; this property holds the value that the user enters in the text box. This custom property can be edited when the control is in edit mode, along with the standard WebPart control properties.
Imports System Imports System.Collections Imports System.ComponentModel Imports System.Drawing Imports System.Security.Permissions Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Namespace Samples.AspNet.VB.Controls <AspNetHostingPermission(SecurityAction.Demand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermission(SecurityAction.InheritanceDemand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class TextDisplayWebPart Inherits WebPart Private _contentText As String = Nothing Private _fontStyle As String = Nothing Private input As TextBox Private DisplayContent As Label Private lineBreak As Literal <Personalizable(), WebBrowsable()> _ Public Property ContentText() As String Get Return _contentText End Get Set(ByVal value As String) _contentText = value End Set End Property Protected Overrides Sub CreateChildControls() Controls.Clear() DisplayContent = New Label() DisplayContent.BackColor = Color.LightBlue DisplayContent.Text = Me.ContentText Me.Controls.Add(DisplayContent) lineBreak = New Literal() lineBreak.Text = "<br />" Controls.Add(lineBreak) input = New TextBox() Me.Controls.Add(input) Dim update As New Button() update.Text = "Set Label Content" AddHandler update.Click, AddressOf Me.submit_Click Me.Controls.Add(update) End Sub Private Sub submit_Click(ByVal sender As Object, _ ByVal e As EventArgs) ' Update the label string. If input.Text <> String.Empty Then _contentText = input.Text + "<br />" input.Text = String.Empty DisplayContent.Text = Me.ContentText End If End Sub End Class End Namespace
The third part of the code example is the Web page. Notice that the <asp:editorzone> element for the page contains declarations for three EditorPart controls. Two of these controls become part of a custom EditorPartCollection object that is created when the Button1_Click method executes.
<%@ page language="vb" %> <%@ register TagPrefix="uc1" TagName="DisplayModeMenu" Src="DisplayModevb.ascx" %> <%@ register tagprefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="TextDisplayWebPartVB" %> <script runat="server"> Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Dim list As New ArrayList(2) list.Add(AppearanceEditorPart1) list.Add(PropertyGridEditorPart1) ' Pass an ICollection object to the constructor. Dim myParts As New EditorPartCollection(list) Dim editor As EditorPart For Each editor In myParts editor.BackColor = System.Drawing.Color.LightBlue editor.Description = "My " + editor.DisplayTitle + " editor." Next editor ' Use the IndexOf property to locate an EditorPart control. Dim propertyGridPart As Integer = _ myParts.IndexOf(PropertyGridEditorPart1) myParts(propertyGridPart).ChromeType = PartChromeType.TitleOnly ' Use the Contains method to see if an EditorPart exists. If Not myParts.Contains(LayoutEditorPart1) Then LayoutEditorPart1.BackColor = System.Drawing.Color.LightYellow End If ' Use the CopyTo method to create an array of EditorParts. Dim partArray(2) As EditorPart partArray(0) = LayoutEditorPart1 myParts.CopyTo(partArray, 1) Label1.Text = "<h3>EditorParts in Custom Array</h3>" Dim ePart As EditorPart For Each ePart In partArray Label1.Text += ePart.Title + "<br />" Next ePart End Sub </script> <html> <head id="Head1" runat="server"> <title> Text Display WebPart with AppearanceEditorPart </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"> <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" /> <asp:PropertyGridEditorPart ID="PropertyGridEditorPart1" runat="server" /> </ZoneTemplate> </asp:EditorZone> <asp:Button ID="Button1" runat="server" Text="Create EditorPartCollection" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="" /> </form> </body> </html>
When you load the page in a browser, you can switch the page into edit mode by selecting Edit in the Display Mode drop-down list control. You can 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 user interface (UI) is visible, you can see all the EditorPart controls. You click the Create EditorPartCollection button to see the effects on the EditorPart controls that are created by the code that is manipulating the EditorPartCollection object. Also, notice that the PropertyGridEditorPart control allows you to edit the custom TextDisplayWebPart.ContentText property. This is possible because the property is marked with a WebBrowsable attribute in the source code for the control. If you update the property value in the editing UI, you must return the page to normal browse mode to see the effect of updating the TextDisplayWebPart.ContentText property.
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
System.Collections.ReadOnlyCollectionBase
System.Web.UI.WebControls.WebParts.EditorPartCollection
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.