WebPartCollection 建構函式

定義

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

多載

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

例外狀況

webPartsnull

webParts 集合中的物件為 null

-或- webParts 集合中的物件不屬於 WebPart 型別。

範例

下列程式碼範例示範如何在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 物件迴圈,並替代地將控制項最小化,或將它們傳回正常。 或者,如果您重複按一下 [切換專案符號清單1 標題 ] 按鈕,最上方控制項的標題會變更為替代值。

備註

WebPartCollection 構函式會傳入 控制項集合, WebPartCollection 以初始化 類別的 WebPart 實例。

WebPartCollection雖然物件是唯讀的,但沒有任何方法可將個別控制項加入其中,但您可以建立自己的 ICollection 控制項集合,並將它傳遞至 WebPartCollection 建構函式。 這可讓您建立自訂集合,並對其執行大量作業。 您也可以存取集合中的基礎控制項,並以程式設計方式變更其屬性值。

另請參閱

適用於