WebPartCollection 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
包含用于跟踪和管理相关控件组的 WebPart 控件的集合。 此类不能被继承。
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 对象。 此示例包含三个部分:
分部类中页面的代码。
包含控件的网页。
说明该示例在浏览器中的工作原理。
代码示例的第一部分包含分部类中页面的代码。 请注意, Button1_Click 方法创建一个 WebPartCollection 对象,该对象包含 属性中WebPartManager.WebParts引用的所有WebPart控件,其中包括页面上的所有WebPart控件。 方法循环访问所有控件,并切换每个控件的 ChromeState 属性,以确定该控件是正常控件还是最小化控件。
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
代码示例的第二部分是包含控件的网页。 请注意,中 WebPartZone1 声明的控件是标准 ASP.NET 服务器控件,但由于它们在运行时包装为 GenericWebPart 控件,并且 GenericWebPart 类继承自 WebPart 类,因此控件在运行时自动被视为 WebPart 控件,因此包含在 对象中 WebPartCollection 。
<%@ 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 对象,并交替将控件最小化或使其恢复正常。 或者,如果反复单击“ 切换 BulletedList1 标题 ”按钮,则最上面的控件的标题将更改为备用值。
注解
类 WebPartCollection 是控件的只读集合,通常由 WebPartZoneBase 和 WebPartManager 控件用来管理控件集 WebPart 。
WebPartManager控件使用 WebPartCollection 对象来保留页面上所有WebPart控件的列表,而 WebPartZoneBase 控件使用 WebPartCollection 对象来跟踪WebPart它所包含的控件。
注意
集合 WebPartCollection 包含 WebPart 控件和其他服务器控件 (,例如用户控件、自定义控件和 ASP.NET 控件) 放置在区域中 WebPartZoneBase 并用作 Web 部件应用程序的一部分。 因此,例如,如果在页面中有一个WebPartZone区域,并在其中声明了自定义WebPart控件和 ASP.NET Calendar 控件,则这两个控件都将位于 由 属性引用的WebParts集合中WebPartCollection。
对象 WebPartCollection 存在,以便 Web 部件控件集可以与强类型集合一起使用。 同样,如果要对一组WebPart控件执行大规模操作,可以使用 属性获取对 对象的WebPartCollectionWebParts引用。 例如,你可能想要循环访问页面上的所有 WebPart 控件,并通过某种方式更改其外观。 即使 对象是只读的 WebPartCollection ,也可以对集合中引用的基础控件的属性进行编程更改。
构造函数
| WebPartCollection() | 
		 初始化 WebPartCollection 类的空的新实例。  | 
        	
| WebPartCollection(ICollection) | 
		 通过传入 WebPartCollection 控件的 ICollection 集合来初始化 WebPart 对象的新实例。  | 
        	
属性
| Count | 
		 获取 ReadOnlyCollectionBase 实例中包含的元素数。 (继承自 ReadOnlyCollectionBase) | 
        	
| InnerList | 
		 获取 ReadOnlyCollectionBase 实例中包含的元素的列表。 (继承自 ReadOnlyCollectionBase) | 
        	
| Item[Int32] | 
		 基于集合中的位置返回集合的成员。  | 
        	
| Item[String] | 
		 基于唯一字符串标识符返回集合的成员。  | 
        	
方法
| Contains(WebPart) | 
		 返回一个值,该值指示集合中是否存在特定控件。  | 
        	
| CopyTo(WebPart[], Int32) | 
		 将集合复制到 WebPart 对象的数组。  | 
        	
| Equals(Object) | 
		 确定指定对象是否等于当前对象。 (继承自 Object) | 
        	
| GetEnumerator() | 
		 返回循环访问 ReadOnlyCollectionBase 实例的枚举器。 (继承自 ReadOnlyCollectionBase) | 
        	
| GetHashCode() | 
		 作为默认哈希函数。 (继承自 Object) | 
        	
| GetType() | 
		 获取当前实例的 Type。 (继承自 Object) | 
        	
| IndexOf(WebPart) | 
		 返回集合中特定成员的位置。  | 
        	
| 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。  |