語言

WebPartCollection 建構函式

定義

初始化 WebPartCollection 類別的新執行個體。

多載

名稱 Description
WebPartCollection()

初始化一個空的新類別實例 WebPartCollection 。

WebPartCollection(ICollection)

透過傳遞WebPartCollection一組ICollection控制項初始化物件的新WebPart實例。

WebPartCollection()

初始化一個空的新類別實例 WebPartCollection 。

public:
 WebPartCollection();
public WebPartCollection();
Public Sub New ()

備註

WebPartCollection建構子初始化該類別的空實例WebPartCollection。 物件本身是唯讀的,沒有方法可以新增個別 WebPart 控制項;因此,你很少有機會使用這個建構子。

另請參閱

適用於

WebPartCollection(ICollection)

透過傳遞WebPartCollection一組ICollection控制項初始化物件的新WebPart實例。

public:
 WebPartCollection(System::Collections::ICollection ^ webParts);
public WebPartCollection(System.Collections.ICollection webParts);
new System.Web.UI.WebControls.WebParts.WebPartCollection : System.Collections.ICollection -> System.Web.UI.WebControls.WebParts.WebPartCollection
Public Sub New (webParts As ICollection)

參數

webParts
ICollection

一個 ICollection 控制系統 WebPart 。

例外狀況

webParts 是 null。

集合中的 webParts 一個物件是 null。

-或-

集合中的 webParts 物件不是型別 WebPart。

範例

以下程式碼範例示範了在 Web Parts 頁面上使用建 WebPartCollection 構子。 此範例分為三部分:

  • 頁面的程式碼屬於部分類別。

  • 包含控制裝置的網頁。

  • 說明這個範例在瀏覽器中的運作方式。

程式碼範例的第一部分包含該頁面的部分類別程式碼。 請注意,該 Button1_Click 方法會建立一個 WebPartCollection 物件,包含屬性中所有 WebPart 的 WebPartManager.WebParts 控制項,而該屬性也包含頁面上的所有 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>

在瀏覽器載入頁面後,點擊 Toggle ChromeState 按鈕,會發現部分類別的程式碼會循環物件 WebPartCollection ,並交替最小化控制項或恢復正常。 或者,如果你反覆點擊 Toggle BulletedList1 標題 按鈕,最上方控制項的標題會被改成其他值。

備註

WebPartCollection建構子透過傳遞一組WebPartCollection控制項來初始化該WebPart類別的實例。

雖然物件 WebPartCollection 是唯讀,且沒有方法新增個別控制項,但你可以建立自己的 ICollection 控制項集合,並交給 WebPartCollection 建構子。 這讓你能建立自訂收藏並對其執行批量操作。 你也可以存取集合中的底層控制項,並以程式化方式更改它們的屬性值。

另請參閱

適用於