WebPartCollection WebPartCollection WebPartCollection WebPartCollection Class

定义

包含用于跟踪和管理相关控件组的 WebPart 控件的集合。Contains a collection of WebPart controls used to track and manage related groups of controls. 此类不能被继承。This class cannot be inherited.

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

示例

下面的代码示例演示如何在 Web 部件页WebPartCollection上使用对象。The following code example demonstrates the use of the WebPartCollection object on a Web Parts page. 此示例包含三个部分:This example has three parts:

  • 分部类中页的代码。The code for the page in a partial class.

  • 包含控件的网页。The Web page that contains the controls.

  • 说明如何在浏览器中工作。A description of how the example works in a browser.

此代码示例的第一部分包含分部类中页的代码。The first part of the code example contains the code for the page in a partial class. WebPartManager.WebParts WebPartCollection WebPart请注意, WebPart方法会创建一个对象, 该对象包含属性中引用的所有控件, 其中包括页面上的所有控件。 Button1_ClickNote that the Button1_Click method creates a WebPartCollection object that consists of all the WebPart controls referenced in the WebPartManager.WebParts property, which includes all the WebPart controls on the page. 方法循环访问所有控件, 并切换每个控件的ChromeState属性, 以确定控件是正常还是最小化。The method iterates through all the controls, and toggles each control's ChromeState property, which determines whether that control is normal or minimized.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class webpartcollectioncs : System.Web.UI.Page
{
  protected void Button1_Click(object sender, EventArgs e)
  {

    WebPartCollection partCollection = mgr1.WebParts;

    foreach (WebPart part in partCollection)
    {
      if (part.ChromeState != PartChromeState.Minimized)
        part.ChromeState = PartChromeState.Minimized;
      else
        part.ChromeState = PartChromeState.Normal;
    }
  }
  protected void Button2_Click(object sender, EventArgs e)
  {
    WebPartCollection partCollection = WebPartZone1.WebParts;

    if (partCollection[0].Title == "My Link List")
      partCollection[0].Title = "Favorite Links";
    else
      partCollection[0].Title = "My Link List";
  }
}
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Partial Public Class webpartcollectionvb

  Inherits System.Web.UI.Page

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim partCollection As WebPartCollection = mgr1.WebParts
    Dim part As WebPart

    For Each part In partCollection
      If part.ChromeState <> PartChromeState.Minimized Then
        part.ChromeState = PartChromeState.Minimized
      Else
        part.ChromeState = PartChromeState.Normal
      End If
    Next

  End Sub

  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim partCollection As WebPartCollection = WebPartZone1.WebParts

    If partCollection(0).Title = "My Link List" Then
      partCollection(0).Title = "Favorite Links"
    Else
      partCollection(0).Title = "My Link List"
    End If

  End Sub

End Class

此代码示例的第二部分是包含控件的网页。The second part of the code example is the Web page that contains the controls. 请注意, WebPartZone1在中声明的控件是标准的 ASP.NET 服务器控件, 但由于它们在GenericWebPart运行时被包装为GenericWebPart控件, 并且该类继承WebPart自类, 因此控件将自动在运行时被视为WebPartCollection 控件,因此包含在对象中。WebPartNotice that the controls declared in WebPartZone1 are standard ASP.NET server controls, but because they are wrapped as GenericWebPart controls at run time, and the GenericWebPart class inherits from the WebPart class, the controls are automatically treated as WebPart controls at run time and therefore are included in the WebPartCollection object.

<%@ Page Language="C#" 
  Codefile="webpartcollection.cs" 
  Inherits="webpartcollectioncs" %>

<!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">
    <div>
      <asp:WebPartManager ID="mgr1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            ID="BulletedList1" 
            Runat="server"
            DisplayMode="HyperLink" 
            Title="Favorite Links" >
            <asp:ListItem Value="http://msdn.microsoft.com">
              MSDN
            </asp:ListItem>
            <asp:ListItem Value="http://www.asp.net">
              ASP.NET
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
              MSN
            </asp:ListItem>
          </asp:BulletedList>
          <br />
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
    </div>
    <hr />
    <asp:Button ID="Button1" runat="server" Width="200"
      Text="Toggle ChromeState" OnClick="Button1_Click" />
    <br />
    <asp:Button ID="Button2" runat="server" Width="200"
        Text="Toggle BulletedList1 Title" 
        OnClick="Button2_Click"/>
    </form>
</body>
</html>
<%@ Page Language="vb"
  Codefile="webpartcollection.vb" 
  Inherits="webpartcollectionvb" %>

<!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 id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            ID="BulletedList1" 
            Runat="server"
            DisplayMode="HyperLink" 
            Title="Favorite Links" >
            <asp:ListItem Value="http://msdn.microsoft.com">
              MSDN
            </asp:ListItem>
            <asp:ListItem Value="http://www.asp.net">
              ASP.NET
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
              MSN
            </asp:ListItem>
          </asp:BulletedList>
          <br />
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
    </div>
    <hr />
    <asp:Button ID="Button1" runat="server" Width="200"
      Text="Toggle ChromeState" OnClick="Button1_Click" />
    <br />
    <asp:Button ID="Button2" runat="server" Width="200"
        Text="Toggle BulletedList1 Title" 
        OnClick="Button2_Click"/>
    </form>
</body>
</html>

在浏览器中加载页面并单击 "切换 ChromeState " 按钮后, 分部类中的代码将循环遍历该WebPartCollection对象, 并以其他方式最小化控件或将其返回到正常。After you load the page in a browser and click the Toggle ChromeState button, the code in the partial class loops through the WebPartCollection object and alternately minimizes the controls or returns them to normal. 或者, 如果反复单击 "切换 BulletedList1 标题" 按钮, 最上面控件的标题将更改为替换值。Or, if you repeatedly click the Toggle BulletedList1 Title button, the title of the uppermost control is changed to alternate values.

注解

类是控件的只读集合, 通常由WebPartZoneBaseWebPartManager控件用于管理WebPart控件集。 WebPartCollectionThe WebPartCollection class is a read-only collection of controls, typically used by WebPartZoneBase and WebPartManager controls to manage sets of WebPart controls.

WebPartZoneBase WebPart WebPartCollection控件使用对象来保存页面上所有控件的列表, 而控件则使用对象跟踪它所包含的WebPart控件。 WebPartCollection WebPartManagerA WebPartManager control uses a WebPartCollection object to keep a list of all the WebPart controls on a page, whereas a WebPartZoneBase control uses a WebPartCollection object to track the WebPart controls that it contains.

备注

集合同时WebPart包含控件和其他服务器控件 (如用户控件、自定义控件和 ASP.NET 控件), 这些控件位于区域中WebPartZoneBase并用作 Web 部件应用程序的一部分。 WebPartCollectionThe WebPartCollection collection contains both WebPart controls and other server controls (such as user controls, custom controls, and ASP.NET controls) that are placed in WebPartZoneBase zones and used as part of Web Parts applications. 例如, 如果您在某个页面中有WebPartZone一个区域, 并且在该区域内声明一个自定义WebPart控件和 ASP.NET Calendar WebPartCollection控件, 则这两个控件都WebParts将位于该属性引用的集合中。So, for example, if you have a WebPartZone zone in a page, and within it you declare a custom WebPart control and the ASP.NET Calendar control, both controls will be in the WebPartCollection collection referenced by the WebParts property.

WebPartCollection对象存在, 以便 Web 部件控件集可以使用强类型集合。The WebPartCollection object exists so that the Web Parts control set can work with strongly typed collections. 同样, 如果要对一组WebPart控件执行批量操作, 则可以使用WebParts属性获取WebPartCollection对对象的引用。Similarly, if you want to perform mass operations on a set of WebPart controls, you can get a reference to a WebPartCollection object using the WebParts property. 例如, 你可能想要循环遍历页面上的WebPart所有控件, 并以某种方式更改它们的外观。For example, you might want to loop through all the WebPart controls on a page and change their appearance in some way. WebPartCollection即使对象是只读的, 你也可以对集合中引用的基础控件的属性进行编程更改。Even though the WebPartCollection object is read-only, you can make programmatic changes to the properties of the underlying controls referenced in the collection.

构造函数

WebPartCollection() WebPartCollection() WebPartCollection() WebPartCollection()

初始化 WebPartCollection 类的空的新实例。Initializes an empty new instance of the WebPartCollection class.

WebPartCollection(ICollection) WebPartCollection(ICollection) WebPartCollection(ICollection) WebPartCollection(ICollection)

通过传入 WebPartCollection 控件的 ICollection 集合来初始化 WebPart 对象的新实例。Initializes a new instance of a WebPartCollection object by passing in an ICollection collection of WebPart controls.

属性

Count Count Count Count

获取包含在 ReadOnlyCollectionBase 实例中的元素数。Gets the number of elements contained in the ReadOnlyCollectionBase instance.

(Inherited from ReadOnlyCollectionBase)
ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

获取一个值,该值指示对 ReadOnlyCollectionBase 对象的访问是否同步(线程安全)。Gets a value indicating whether access to a ReadOnlyCollectionBase object is synchronized (thread safe).

(Inherited from ReadOnlyCollectionBase)
ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

获取一个对象,该对象可用于同步对 ReadOnlyCollectionBase 对象的访问。Gets an object that can be used to synchronize access to a ReadOnlyCollectionBase object.

(Inherited from ReadOnlyCollectionBase)
InnerList InnerList InnerList InnerList

获取 ReadOnlyCollectionBase 实例中包含的元素的列表。Gets the list of elements contained in the ReadOnlyCollectionBase instance.

(Inherited from ReadOnlyCollectionBase)
Item[Int32] Item[Int32] Item[Int32] Item[Int32]

基于集合中的位置返回集合的成员。Returns a member of the collection based on its position in the collection.

Item[String] Item[String] Item[String] Item[String]

基于唯一字符串标识符返回集合的成员。Returns a member of the collection based on a unique string identifier.

方法

Contains(WebPart) Contains(WebPart) Contains(WebPart) Contains(WebPart)

返回一个值,该值指示集合中是否存在特定控件。Returns a value indicating whether a particular control exists in the collection.

CopyTo(WebPart[], Int32) CopyTo(WebPart[], Int32) CopyTo(WebPart[], Int32) CopyTo(WebPart[], Int32)

将集合复制到 WebPart 对象的数组。Copies the collection to an array of WebPart objects.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

返回循环访问 ReadOnlyCollectionBase 实例的枚举器。Returns an enumerator that iterates through the ReadOnlyCollectionBase instance.

(Inherited from ReadOnlyCollectionBase)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IndexOf(WebPart) IndexOf(WebPart) IndexOf(WebPart) IndexOf(WebPart)

返回集合中特定成员的位置。Returns the position of a particular member of the collection.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 ReadOnlyCollectionBase 复制到兼容的一维 ArrayCopies the entire ReadOnlyCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(Inherited from ReadOnlyCollectionBase)

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅