WebPartDisplayModeCollection 类

定义

包含 WebPartDisplayMode 对象的集合。 此类不能被继承。

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

示例

下面的代码示例演示如何使用 WebPartDisplayModeCollection 类。 关键点是,必须继承自 WebPartManager 类并重写 方法, CreateDisplayModes 以便将自定义 WebPartDisplayMode 对象添加到 WebPartDisplayModeCollection 控件 WebPartManager 创建的集合中。

此代码示例包含五个部分:

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

  • 承载其他控件的网页。

  • 驻留在 WebPartZone 网页上的区域中的用户控件,可用于在标签中输入和显示文本。

  • 包含两个控件的源代码文件。 一个是自定义 WebPartManager 控件;另一个是要添加到页面的默认显示模式的自定义 WebPartDisplayMode 对象。

  • 对示例工作原理的说明。

代码示例第一部分的源代码(用于更改显示模式的用户控件)来自另一个主题。 若要运行此代码示例,需要从 演练:更改 Web 部件页上的显示模式 主题中获取用户控件的 .ascx 文件,并将该文件放置在此代码示例中 .aspx 页所在的文件夹中。

该示例的第二部分是网页。 它包含两个 WebPartZone 控件,即用户控件和自定义 WebPartManager 控件。 Register请注意页面顶部的 指令,用于引用已编译控件的用户控件和命名空间。

<%@ Page Language="C#" %>
<%@ Register Src="TextDisplaycs.ascx" 
    TagName="TextDisplay" 
    TagPrefix="uc2" %>
<%@ Register Src="DisplayModeMenuCS.ascx" 
    TagName="DisplayModeMenuCS" 
    TagPrefix="uc1" %>
<%@ Register Namespace="Samples.AspNet.CS.Controls" 
    TagPrefix="sample" 
    Assembly="CustomDisplayModeCS"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <uc1:DisplayModeMenuCS id="menu1" runat="server" />
    <div>
    <sample:NewWebPartManager runat="server" ID="wpgm1" />
    <br />
    <table style="width: 100%">
      <tr valign="top" align="center" >
        <td style="width: 100px; height: 123px">
          <asp:WebPartZone ID="WebPartZone1" runat="server">
            <ZoneTemplate>
              <uc2:TextDisplay ID="TextDisplay1" runat="server" />
            </ZoneTemplate>
          </asp:WebPartZone>
        </td>
        <td style="width: 100px; height: 123px">
          <asp:WebPartZone ID="WebPartZone2" runat="server" />
        </td>
      </tr>
    </table>
    <br />
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register Src="TextDisplayvb.ascx" 
    TagName="TextDisplay" 
    TagPrefix="uc2" %>
<%@ Register Src="DisplayModeMenuVB.ascx" 
    TagName="DisplayModeMenuVB" 
    TagPrefix="uc1" %>
<%@ Register Namespace="Samples.AspNet.VB.Controls" 
    TagPrefix="sample" 
    Assembly="CustomDisplayModeVB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <uc1:DisplayModeMenuVB id="menu1" runat="server" />
    <div>
    <sample:NewWebPartManager runat="server" ID="wpgm1" />
    <br />
    <table style="width: 100%">
      <tr valign="top" align="center" >
        <td style="width: 100px; height: 123px">
          <asp:WebPartZone ID="WebPartZone1" runat="server">
            <ZoneTemplate>
              <uc2:TextDisplay ID="TextDisplay1" runat="server" />
            </ZoneTemplate>
          </asp:WebPartZone>
        </td>
        <td style="width: 100px; height: 123px">
          <asp:WebPartZone ID="WebPartZone2" runat="server" />
        </td>
      </tr>
    </table>
    <br />
    </div>
    </form>
</body>
</html>

该示例的第三部分是用于输入和显示文本的用户控件。 请注意,它使用 MultiView 控件创建用户界面 (UI) 的多个视图。 一个视图随按钮一起显示,另一个视图不显示。 请注意,在重写 OnPreRender 的 方法中,代码检查页面当前是否处于自定义显示模式,如果是,则显示用户控件的第一个视图,其中包括 按钮。 例如,如果页面未处于自定义显示模式 (,则页面处于浏览或设计模式) ,则隐藏按钮。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Control Language="C#" %>
<%@ Import Namespace="Samples.AspNet.CS.Controls" %>
    
<script runat="server">
  private string textContent;

  [Personalizable]
  public string TextContent
  {
    get { return textContent; }
    set { textContent = value; }
  }

  protected override void OnPreRender(EventArgs e)
  {
    Label1.Text = this.textContent;
    int viewIndex = 0;

    WebPartManager wpmg = 
      WebPartManager.GetCurrentWebPartManager(this.Page);
    NewWebPartManager myNewWpmg = wpmg as NewWebPartManager;
    if (myNewWpmg != null)
    {
      WebPartDisplayMode mode = 
        myNewWpmg.SupportedDisplayModes[myNewWpmg.InLineEditDisplayMode.Name];
      if (mode != null && myNewWpmg.DisplayMode == mode)
      {
        viewIndex = 1;
      }
    }
    this.MultiView1.ActiveViewIndex = viewIndex;

  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    this.TextContent = TextBox1.Text;

    WebPartManager wpmg = 
      WebPartManager.GetCurrentWebPartManager(this.Page);
    WebPartDisplayMode mode = 
      wpmg.SupportedDisplayModes[WebPartManager.BrowseDisplayMode.Name];
    if (mode != null)
      wpmg.DisplayMode = mode;
  }
  
</script>
<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label" />
    </asp:View>
    <asp:View ID="View2" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" OnClick="Button1_Click" 
          runat="server" Text="Button" />
    </asp:View>
</asp:MultiView>
<%@ Control Language="vb" %>
<%@ Import Namespace="Samples.AspNet.VB.Controls" %>
    
<script runat="server">

  Private _textContent As String

  <Personalizable()> _
  Public Property TextContent() As String
    Get
      Return _textContent
    End Get
    Set(ByVal value As String)
      _textContent = Value
    End Set
  End Property
 
  Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
    Label1.Text = Me.TextContent
    Dim viewIndex As Integer = 0
      
    Dim wpmg As WebPartManager = _
      WebPartManager.GetCurrentWebPartManager(Me.Page)
    Dim myNewWpmg As NewWebPartManager = _
      CType(wpmg, NewWebPartManager)
    If Not (myNewWpmg Is Nothing) Then
      Dim mode As WebPartDisplayMode = _
        myNewWpmg.SupportedDisplayModes(myNewWpmg.InLineEditDisplayMode.Name)
      If Not (mode Is Nothing) AndAlso _
        myNewWpmg.DisplayMode Is mode Then
        viewIndex = 1
      End If
    End If
    Me.MultiView1.ActiveViewIndex = viewIndex

  End Sub

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As EventArgs)
    
    Me.TextContent = TextBox1.Text
    Dim wpmg As WebPartManager = _
      WebPartManager.GetCurrentWebPartManager(Me.Page)
    Dim mode As WebPartDisplayMode = _
      wpmg.SupportedDisplayModes(WebPartManager.BrowseDisplayMode.Name)
    If Not (mode Is Nothing) Then
      wpmg.DisplayMode = mode
    End If

  End Sub
  
</script>
<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label" />
    </asp:View>
    <asp:View ID="View2" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" OnClick="Button1_Click" 
          runat="server" Text="Button" />
    </asp:View>
</asp:MultiView>

该示例的第四部分是两个自定义类的源文件。 请注意,自定义 WebPartManager 类重写 CreateDisplayModes 方法,调用基方法以添加所有默认显示模式,然后添加自定义显示模式。 自定义显示模式类 InLineEditDisplayMode只是继承自 WebPartDisplayMode,在构造函数中设置显示模式的名称,并重写一些基属性以建立自定义显示的特征。

若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放在站点的“App_Code”文件夹中,并在运行时对其进行动态编译。 有关演示如何编译的演练,请参阅 演练:开发和使用自定义 Web 服务器控件

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Permissions;
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 NewWebPartManager : WebPartManager 
  {
    private static readonly WebPartDisplayMode _inLineEditDisplayMode =
      new InlineWebPartEditDisplayMode();

    public NewWebPartManager() {}

    protected override WebPartDisplayModeCollection CreateDisplayModes() 
    {
      WebPartDisplayModeCollection displayModes = 
        base.CreateDisplayModes();
      displayModes.Add(_inLineEditDisplayMode);
      return displayModes;
    }

    public WebPartDisplayMode InLineEditDisplayMode
    {
      get { return _inLineEditDisplayMode; }
    }

    private sealed class InlineWebPartEditDisplayMode : WebPartDisplayMode
    {
      public InlineWebPartEditDisplayMode()
        : base("Inline Edit Display")
      {
      }
      public override bool AllowPageDesign
      {
        get { return true; }
      }
      public override bool RequiresPersonalization
      {
        get { return true; }
      }
      public override bool ShowHiddenWebParts
      {
        get { return false; }
      }
      public override bool AssociatedWithToolZone
      {
        get { return false; }
      }
      public override bool IsEnabled(WebPartManager webPartManager)
      {
        return true;
      }
    }
  }
}
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
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 NewWebPartManager
    Inherits WebPartManager
    Private Shared _inLineEditDisplayMode As WebPartDisplayMode = _
      New InlineWebPartEditDisplayMode()

    Public Sub New()
    End Sub

    Protected Overrides Function CreateDisplayModes() As WebPartDisplayModeCollection
      Dim displayModes As WebPartDisplayModeCollection = MyBase.CreateDisplayModes()
      displayModes.Add(_inLineEditDisplayMode)
      Return displayModes

    End Function 

    Public ReadOnly Property InLineEditDisplayMode() As WebPartDisplayMode
        Get
            Return _inLineEditDisplayMode
        End Get
    End Property

    Private NotInheritable Class InlineWebPartEditDisplayMode
      Inherits WebPartDisplayMode

      Public Sub New()
        MyBase.New("Inline Edit Display")
      End Sub

      Public Overrides ReadOnly Property AllowPageDesign() As Boolean
        Get
          Return True
        End Get
      End Property

      Public Overrides ReadOnly Property RequiresPersonalization() _
        As Boolean
        Get
          Return True
        End Get
      End Property

      Public Overrides ReadOnly Property ShowHiddenWebParts() As Boolean
        Get
          Return False
        End Get
      End Property

      Public Overrides ReadOnly Property AssociatedWithToolZone() _
        As Boolean
        Get
          Return False
        End Get
      End Property

      Public Overrides Function IsEnabled(ByVal webPartManager _
        As WebPartManager) As Boolean

        Return True

      End Function

    End Class

  End Class

End Namespace

若要运行代码示例,请在浏览器中加载页面。 请注意,页面当前处于浏览模式,并且没有按钮可见。 使用 “显示模式 ”下拉列表控件,将页面更改为 “内联编辑显示 模式”,请注意,按钮现在在下方的用户控件中可见。 添加一些文本,然后单击按钮更新控件。 请注意,页面显示将返回到浏览模式,现在将显示你输入的文本,并且按钮再次隐藏,因为页面未处于自定义显示模式。

注解

WebPartDisplayModeCollection 旨在包含 对象的集合 WebPartDisplayMode 。 它主要由 WebPartManager 控件用来管理对象的集合 WebPartDisplayMode

显示模式是网页的特殊视图,在被分配为 属性上的 WebPartManager.DisplayMode 当前显示模式时显示该网页。 显示模式在 Web 部件控件集中用于创建页面视图,用户可以在其中执行特殊任务,例如编辑控件或重新排列页面布局。 控件WebPartManager定义多种显示模式,包括 BrowseDisplayMode、、DesignDisplayModeEditDisplayModeCatalogDisplayModeConnectDisplayMode。 显示模式的集合由 WebPartManager.DisplayModes 属性引用。

在使用 Web 部件控件的任何特定网页上,只有某些显示模式可用。 默认浏览模式和设计模式几乎始终可用,但仅当页面上存在相应的区域类型时,其他显示模式才可用。 有关详细信息,请参阅 WebPartDisplayMode 类概述。

控件 WebPartManager 使用其 SupportedDisplayModes 属性跟踪页面上的可用显示模式。 此属性引用一个 WebPartDisplayModeCollection 对象,该对象包含所有受支持的显示模式。

WebPartDisplayModeCollection 没有公开的构造函数,因此无法创建自己的新实例。 如果创建自定义对象并希望它成为控件中WebPartManager支持的显示模式集合的一WebPartDisplayMode部分,则必须从 WebPartManager 类继承,重写 CreateDisplayModes 方法,调用基方法以创建集合,然后使用其 Add 方法将任何自定义显示模式添加到集合。

WebPartDisplayModeCollection 具有两个公共属性。 属性 IsReadOnly 是一个只读属性,指示集合是否为只读。 重载 Item[] 属性提供对集合成员的访问权限。

WebPartDisplayModeCollection 还具有多个方法。 Add已提到的 方法使你能够将 对象添加到WebPartDisplayMode集合。 方法 Contains 确定集合中是否存在特定的显示模式。 方法 CopyTo 将集合复制到 对象的数组中。 方法 IndexOf 返回集合中特定显示模式的索引。 最后, Insert 使用 方法可以在集合中的特定索引处插入显示模式对象。

属性

Capacity

获取或设置 CollectionBase 可包含的元素数。

(继承自 CollectionBase)
Count

获取 CollectionBase 实例中包含的元素数。 不能重写此属性。

(继承自 CollectionBase)
InnerList

获取一个 ArrayList,它包含 CollectionBase 实例中元素的列表。

(继承自 CollectionBase)
IsReadOnly

获取一个值,该值指示集合是否为只读。

Item[Int32]

根据索引获取集合中的特定成员。

Item[String]

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

List

获取一个 IList,它包含 CollectionBase 实例中元素的列表。

(继承自 CollectionBase)

方法

Add(WebPartDisplayMode)

WebPartDisplayMode 对象添加到集合中。

Clear()

CollectionBase 实例移除所有对象。 不能重写此方法。

(继承自 CollectionBase)
Contains(WebPartDisplayMode)

返回一个值,该值指示集合中是否存在特定的 WebPartDisplayMode 对象。

CopyTo(WebPartDisplayMode[], Int32)

将集合复制到 WebPartDisplayMode 对象的数组。

Equals(Object)

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

(继承自 Object)
GetEnumerator()

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

(继承自 CollectionBase)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
IndexOf(WebPartDisplayMode)

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

Insert(Int32, WebPartDisplayMode)

WebPartDisplayMode 对象插入到集合中的指定索引位置处。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnClear()

清除 CollectionBase 实例的内容时执行其他自定义进程。

(继承自 CollectionBase)
OnClearComplete()

在清除 CollectionBase 实例的内容之后执行其他自定义进程。

(继承自 CollectionBase)
OnInsert(Int32, Object)

在向 CollectionBase 实例中插入新元素之前执行其他自定义进程。

(继承自 CollectionBase)
OnInsertComplete(Int32, Object)

在向 CollectionBase 实例中插入新元素之后执行其他自定义进程。

(继承自 CollectionBase)
OnRemove(Int32, Object)

当从 CollectionBase 实例移除元素时执行其他自定义进程。

(继承自 CollectionBase)
OnRemoveComplete(Int32, Object)

在从 CollectionBase 实例中移除元素之后执行其他自定义进程。

(继承自 CollectionBase)
OnSet(Int32, Object, Object)

当在 CollectionBase 实例中设置值之前执行其他自定义进程。

(继承自 CollectionBase)
OnSetComplete(Int32, Object, Object)

当在 CollectionBase 实例中设置值后执行其他自定义进程。

(继承自 CollectionBase)
OnValidate(Object)

当验证值时执行其他自定义进程。

(继承自 CollectionBase)
RemoveAt(Int32)

移除 CollectionBase 实例的指定索引处的元素。 此方法不可重写。

(继承自 CollectionBase)
ToString()

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

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

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

(继承自 CollectionBase)
ICollection.IsSynchronized

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

(继承自 CollectionBase)
ICollection.SyncRoot

获取可用于同步对 CollectionBase 的访问的对象。

(继承自 CollectionBase)
IList.Add(Object)

将对象添加到 CollectionBase 的结尾处。

(继承自 CollectionBase)
IList.Contains(Object)

确定 CollectionBase 是否包含特定元素。

(继承自 CollectionBase)
IList.IndexOf(Object)

搜索指定的 Object,并返回整个 CollectionBase 中第一个匹配项的从零开始的索引。

(继承自 CollectionBase)
IList.Insert(Int32, Object)

将元素插入 CollectionBase 的指定索引处。

(继承自 CollectionBase)
IList.IsFixedSize

获取一个值,该值指示 CollectionBase 是否具有固定大小。

(继承自 CollectionBase)
IList.IsReadOnly

获取一个值,该值指示 CollectionBase 是否为只读。

(继承自 CollectionBase)
IList.Item[Int32]

获取或设置指定索引处的元素。

(继承自 CollectionBase)
IList.Remove(Object)

CollectionBase 中移除特定对象的第一个匹配项。

(继承自 CollectionBase)

扩展方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

另请参阅