EditorPartCollection 类

定义

包含一个 EditorPart 控件集合,这些控件用于编辑 WebPart 控件的属性、布局、外观和行为。 此类不能被继承。

public ref class EditorPartCollection sealed : System::Collections::ReadOnlyCollectionBase
public sealed class EditorPartCollection : System.Collections.ReadOnlyCollectionBase
type EditorPartCollection = class
    inherit ReadOnlyCollectionBase
Public NotInheritable Class EditorPartCollection
Inherits ReadOnlyCollectionBase
继承
EditorPartCollection

示例

下面的代码示例演示了 EditorPartCollection 类的多个用法。 此代码示例有四个部分:

  • 用户控件,可用于更改Web 部件页面上的显示模式。

  • 名为的自定义WebPart控件的类,该类在网页中引用,并由控件编辑EditorPartTextDisplayWebPart

  • 引用控件的TextDisplayWebPart网页包含一个EditorZone控件,其中包含区域中声明的Web 部件控件集中的EditorPart多个控件,并包含一些用于创建和操作EditorPartCollection对象的事件驱动代码。

  • 说明在浏览器中加载代码示例时如何工作。

此代码示例的第一部分是允许用户更改网页上的显示模式的用户控件。 有关此控件中显示模式和源代码的说明的详细信息,请参阅演练:更改Web 部件页面上的显示模式

<%@ 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, 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: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" 
    AssociatedControlID="DisplayModeDropdown"/>
  <asp:DropDownList ID="DisplayModeDropdown" 
    runat="server"  
    AutoPostBack="true" 
    Width="120"
    OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
  </asp:Panel>
</div>
<%@ 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" 
    AssociatedControlID="DisplayModeDropdown"/>
  <asp:DropDownList ID="DisplayModeDropdown" 
    runat="server"  
    AutoPostBack="true" 
    Width="120"
    OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
  </asp:Panel>
</div>

代码示例的第二部分是 TextDisplayWebPart 控件。 若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放在站点的App_Code文件夹中,该文件夹中将在运行时动态编译。 有关演示这两种编译方法的演练,请参阅 演练:开发和使用自定义 Web 服务器控件

请注意,控件具有一 ContentText个名为的属性;此属性保存用户在文本框中输入的值。 当控件处于编辑模式以及标准 WebPart 控件属性时,可以编辑此自定义属性。

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;
    TextBox input;
    Label DisplayContent;
    Literal lineBreak;

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

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      DisplayContent.BackColor = Color.LightBlue;
      DisplayContent.Text = this.ContentText;
      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 (!string.IsNullOrEmpty(input.Text))
      {
        _contentText = input.Text + @"<br />";
        input.Text = String.Empty;
        DisplayContent.Text = this.ContentText;
      }
    }
  }
}
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

代码示例的第三部分是网页。 请注意, <asp:editorzone> 页面的元素包含三 EditorPart 个控件的声明。 其中两个控件成为执行方法时Button1_Click创建的自定义EditorPartCollection对象的一部分。

<%@ page language="c#" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenu" 
  Src="DisplayModecs.ascx" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.CS.Controls" 
  Assembly="TextDisplayWebPartCS" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
  {
    ArrayList list = new ArrayList(2);
    list.Add(AppearanceEditorPart1);
    list.Add(PropertyGridEditorPart1);
    // Pass an ICollection object to the constructor.
    EditorPartCollection myParts = new EditorPartCollection(list);
    foreach (EditorPart editor in myParts)
    {
      editor.BackColor = System.Drawing.Color.LightBlue;
      editor.Description = "My " + editor.DisplayTitle + " editor.";
    }

    // Use the IndexOf property to locate an EditorPart control.
    int propertyGridPart = myParts.IndexOf(PropertyGridEditorPart1);
    myParts[propertyGridPart].ChromeType = PartChromeType.TitleOnly;

    // Use the Contains method to see if an EditorPart exists.
    if(!myParts.Contains(LayoutEditorPart1))
      LayoutEditorPart1.BackColor = System.Drawing.Color.LightYellow;
    
    // Use the CopyTo method to create an array of EditorParts.
    EditorPart[] partArray = new EditorPart[3];
    partArray[0] = LayoutEditorPart1;
    myParts.CopyTo(partArray,1);
    Label1.Text = "<h3>EditorParts in Custom Array</h3>";
    foreach (EditorPart ePart in partArray)
    {
      Label1.Text += ePart.Title + "<br />";
    }

  }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <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>
<%@ page language="vb" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenu" 
  Src="DisplayModevb.ascx" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.VB.Controls" 
  Assembly="TextDisplayWebPartVB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
  <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>

在浏览器中加载页面时,可以通过在 “显示模式”下拉列表控件中选择“编辑”将页面切换到编辑模式。 可以在控件的标题栏中 TextDisplayWebPart 单击谓词菜单 (向下箭头) ,然后单击“ 编辑 ”以编辑控件。 编辑用户界面 (UI) 可见时,可以看到所有 EditorPart 控件。 单击“创建 EditorPartCollection”按钮可查看由操作EditorPartCollection对象的代码创建的控件的影响EditorPart。 另请注意,控件 PropertyGridEditorPart 允许编辑自定义 TextDisplayWebPart.ContentText 属性。 这是可能的,因为该属性在控件的源代码中标记了一个 WebBrowsable 属性。 如果在编辑 UI 中更新属性值,则必须将页面返回到普通浏览模式,以查看更新 TextDisplayWebPart.ContentText 属性的效果。

注解

EditorPartCollection 类是控件的 EditorPart 只读集合,通常由 EditorZoneBase 区域用来跟踪区域包含的 EditorPart 控件集。

当Web 部件页面进入编辑模式,并且用户选择要编辑的控件时,编辑过程将开始。 区域创建一个新 EditorPartCollection 对象,该对象由区域包含的 EditorPart 控件组成。 在编辑过程中的各个阶段,区域访问EditorPartCollection对象以在集合中的控件与当前正在编辑的WebPart控件之间EditorPart保存或检索属性值。

例如,如果需要对一组EditorPart控件执行一些大规模操作,则可以为自己的编程用途创建EditorPartCollection控件集合。 即使对象是只读的 EditorPartCollection ,也可以对集合中引用的基础控件的属性进行编程更改。

构造函数

EditorPartCollection()

初始化 EditorPartCollection 类的空的新实例。

EditorPartCollection(EditorPartCollection, ICollection)

通过传入 EditorPartCollection 控件的 EditorPartCollection 集合和其他 EditorPart 控件的 ICollection 集合,初始化 EditorPart 类的新实例。

EditorPartCollection(ICollection)

通过传入 EditorPartCollection 控件的 ICollection 集合,初始化 EditorPart 类的新实例。

字段

Empty

引用集合的一个空的静态只读实例。

属性

Count

获取 ReadOnlyCollectionBase 实例中包含的元素数。

(继承自 ReadOnlyCollectionBase)
InnerList

获取 ReadOnlyCollectionBase 实例中包含的元素的列表。

(继承自 ReadOnlyCollectionBase)
Item[Int32]

根据唯一标识符返回集合中的特定成员。

方法

Contains(EditorPart)

返回一个值,该值指示集合中是否存在特定控件。

CopyTo(EditorPart[], Int32)

将集合复制到 EditorPart 控件的数组。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetEnumerator()

返回循环访问 ReadOnlyCollectionBase 实例的枚举器。

(继承自 ReadOnlyCollectionBase)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
IndexOf(EditorPart)

返回集合中特定成员的位置。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 ReadOnlyCollectionBase 复制到兼容的一维 Array

(继承自 ReadOnlyCollectionBase)
ICollection.IsSynchronized

获取一个值,该值指示对 ReadOnlyCollectionBase 对象的访问是否同步(线程安全)。

(继承自 ReadOnlyCollectionBase)
ICollection.SyncRoot

获取一个对象,该对象可用于同步对 ReadOnlyCollectionBase 对象的访问。

(继承自 ReadOnlyCollectionBase)

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

另请参阅