WebPartZoneCollection 類別

定義

包含 WebPartZone 控制項的唯讀集合。

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

範例

下列程式碼範例示範 類別的使用 WebPartZoneCollection 。 程式碼範例包含三個部分:兩個程式碼區段,以及如何執行範例的說明。

程式碼範例的第一個部分是包含三 WebPartZone 個控制項的網頁,每個控制項都包含單一伺服器控制項。 雖然這些伺服器控制項都是標準 ASP.NET 控制項,但它們可以在執行時間做為 WebPart 控制項,因為它們會放在 WebPartZone 區域內。

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

<!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>WebPartZoneCollection Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:WebPartManager ID="mgr" runat="server" />
    <asp:Table ID="Table1" runat="server">
      <asp:TableRow>
        <asp:TableCell>
          <asp:Label ID="lblZone1" runat="server" 
            Font-Bold="true"
            AssociatedControlID="WebPartZone1">
            WebPartZone1 Contents
          </asp:Label>
          <asp:WebPartZone ID="WebPartZone1" 
            runat="server" Width="230">
            <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>      
            </ZoneTemplate>
          </asp:WebPartZone>
          <div>
          <asp:Label ID="lblZone2" runat="server" 
            Font-Bold="true"
            AssociatedControlID="WebPartZone2">
            WebPartZone2 Contents
          </asp:Label>
          </div>
          <asp:WebPartZone ID="WebPartZone2" 
            runat="server" Width="230">
            <ZoneTemplate>
              <asp:Calendar ID="Calendar1" 
                runat="server" 
                Title="My Calendar" 
                CatalogIconImageUrl="Mine.gif" />
            </ZoneTemplate>
          </asp:WebPartZone> 
        </asp:TableCell>
        <asp:TableCell VerticalAlign="top"> 
          <asp:Label ID="lblZone3" runat="server" 
            Font-Bold="true" 
            AssociatedControlID="WebPartZone3">
            WebPartZone3 Contents
          </asp:Label>
          <asp:WebPartZone ID="WebPartZone3" runat="server">
            <ZoneTemplate>
              <asp:Table runat="server" ID="table2" 
                Title="Employee Extensions">
                <asp:TableHeaderRow>
                  <asp:TableHeaderCell Scope="Column">
                    Employee Name</asp:TableHeaderCell>
                  <asp:TableHeaderCell Scope="Column">
                    Extension</asp:TableHeaderCell>
                </asp:TableHeaderRow>
                <asp:TableRow>
                  <asp:TableCell>Alberts, Amy</asp:TableCell>
                  <asp:TableCell>x9764</asp:TableCell>
                </asp:TableRow>                
                <asp:TableRow>
                  <asp:TableCell>Hanif, Karim</asp:TableCell>
                  <asp:TableCell>x3240</asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                  <asp:TableCell>Penor, Lori</asp:TableCell>
                  <asp:TableCell>x4165</asp:TableCell>
                </asp:TableRow>
              </asp:Table>
            </ZoneTemplate>
          </asp:WebPartZone>
        </asp:TableCell>
      </asp:TableRow>
    </asp:Table>
    </div>
    <hr />
    <asp:Table ID="Table3" runat="server">
      <asp:TableRow>
        <asp:TableCell>
          <asp:Button ID="Button1" runat="server" 
            Width ="200" OnClick="Button1_Click" 
            Text="Total Zone Count" />
          <br />
          <asp:Button ID="Button2" runat="server" 
            Width ="200" OnClick="Button2_Click"
            Text="Coll. Contains WebPartZone2" />
          <br />
          <asp:Button ID="Button3" runat="server" 
            Width ="200" OnClick="Button3_Click"
            Text="Zone Names from Array" />
          <br />
          <asp:Button ID="Button4" runat="server" 
            Width ="200" OnClick="Button4_Click"
            Text="WebPartZone1 Index" />  
          <br />
          <asp:Button ID="Button5" runat="server" 
            Width ="200" OnClick="Button5_Click"
            Text="Toggle Verb Render Mode" />
        </asp:TableCell>
        <asp:TableCell HorizontalAlign="right" 
          Width="200" VerticalAlign="top">
          <asp:Label ID="Label1" runat="server" 
            Font-Bold="true" />
        </asp:TableCell>  
      </asp:TableRow>
    </asp:Table>
    </form>
</body>
</html>
<%@ Page Language="VB" 
    Codefile="webpartzonecollection_overview.vb" 
    Inherits="webpartzonecollection_overview" %>

<!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>WebPartZoneCollection Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:WebPartManager ID="mgr" runat="server" />
    <asp:Table ID="Table1" runat="server">
      <asp:TableRow>
        <asp:TableCell>
          <asp:Label ID="lblZone1" runat="server" 
            Font-Bold="true"
            AssociatedControlID="WebPartZone1">
            WebPartZone1 Contents
          </asp:Label>
          <asp:WebPartZone ID="WebPartZone1" 
            runat="server" Width="230">
            <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>      
            </ZoneTemplate>
          </asp:WebPartZone>
          <div>
          <asp:Label ID="lblZone2" runat="server" 
            Font-Bold="true"
            AssociatedControlID="WebPartZone2">
            WebPartZone2 Contents
          </asp:Label>
          </div>
          <asp:WebPartZone ID="WebPartZone2" 
            runat="server" Width="230">
            <ZoneTemplate>
              <asp:Calendar ID="Calendar1" 
                runat="server" 
                Title="My Calendar" 
                CatalogIconImageUrl="Mine.gif" />
            </ZoneTemplate>
          </asp:WebPartZone> 
        </asp:TableCell>
        <asp:TableCell VerticalAlign="top"> 
          <asp:Label ID="lblZone3" runat="server" 
            Font-Bold="true" 
            AssociatedControlID="WebPartZone3">
            WebPartZone3 Contents
          </asp:Label>
          <asp:WebPartZone ID="WebPartZone3" runat="server">
            <ZoneTemplate>
              <asp:Table runat="server" ID="table2" 
                Title="Employee Extensions">
                <asp:TableHeaderRow>
                  <asp:TableHeaderCell Scope="Column">
                    Employee Name</asp:TableHeaderCell>
                  <asp:TableHeaderCell Scope="Column">
                    Extension</asp:TableHeaderCell>
                </asp:TableHeaderRow>
                <asp:TableRow>
                  <asp:TableCell>Alberts, Amy</asp:TableCell>
                  <asp:TableCell>x9764</asp:TableCell>
                </asp:TableRow>                
                <asp:TableRow>
                  <asp:TableCell>Hanif, Karim</asp:TableCell>
                  <asp:TableCell>x3240</asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                  <asp:TableCell>Penor, Lori</asp:TableCell>
                  <asp:TableCell>x4165</asp:TableCell>
                </asp:TableRow>
              </asp:Table>
            </ZoneTemplate>
          </asp:WebPartZone>
        </asp:TableCell>
      </asp:TableRow>
    </asp:Table>
    </div>
    <hr />
    <asp:Table ID="Table3" runat="server">
      <asp:TableRow>
        <asp:TableCell>
          <asp:Button ID="Button1" runat="server" 
            Width ="200" OnClick="Button1_Click" 
            Text="Total Zone Count" />
          <br />
          <asp:Button ID="Button2" runat="server" 
            Width ="200" OnClick="Button2_Click"
            Text="Coll. Contains WebPartZone2" />
          <br />
          <asp:Button ID="Button3" runat="server" 
            Width ="200" OnClick="Button3_Click"
            Text="Zone Names from Array" />
          <br />
          <asp:Button ID="Button4" runat="server" 
            Width ="200" OnClick="Button4_Click"
            Text="WebPartZone1 Index" />  
          <br />
          <asp:Button ID="Button5" runat="server" 
            Width ="200" OnClick="Button5_Click"
            Text="Toggle Verb Render Mode" />
        </asp:TableCell>
        <asp:TableCell HorizontalAlign="right" 
          Width="200" VerticalAlign="top">
          <asp:Label ID="Label1" runat="server" 
            Font-Bold="true" />
        </asp:TableCell>  
      </asp:TableRow>
    </asp:Table>
    </form>
</body>
</html>

程式碼範例的第二個部分是包含程式碼以示範類別使用方式 WebPartZoneCollection 的部分類別。 此程式碼會處理網頁的事件。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class webpartzonecollection_overview : System.Web.UI.Page
{

  protected void Button1_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;
    Label1.Text = "WebPartZone Count:  " + mgr.Zones.Count;
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;
    Label1.Text = mgr.Zones.Contains(WebPartZone2).ToString();
  }

  protected void Button3_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;
    WebPartZoneBase[] zoneArray = new WebPartZoneBase[mgr.Zones.Count];
    mgr.Zones.CopyTo(zoneArray, 0);
    Label1.Text = zoneArray[2].ID;
    Label1.Text += ", " + zoneArray[1].ID;
    Label1.Text += ", " + zoneArray[0].ID;
  }

  protected void Button4_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;
    Label1.Text = "WebPartZone1 index:  " + mgr.Zones.IndexOf(WebPartZone1);
  }

  protected void Button5_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;

    WebPartZoneCollection zoneCollection = mgr.Zones;
    foreach (WebPartZone zone in zoneCollection)
    {

      if (zone.WebPartVerbRenderMode == WebPartVerbRenderMode.Menu)
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.TitleBar;
      else
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu;
    }
  }
}
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls


Partial Public Class webpartzonecollection_overview

  Inherits Page

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty
    Label1.Text = "WebPartZone Count:  " & mgr.Zones.Count

  End Sub


  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty
    Label1.Text = mgr.Zones.Contains(WebPartZone2).ToString()

  End Sub

  Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty
    Dim zoneArray(mgr.Zones.Count) As WebPartZoneBase
    mgr.Zones.CopyTo(zoneArray, 0)
    Label1.Text = zoneArray(2).ID
    Label1.Text += ", " & zoneArray(1).ID
    Label1.Text += ", " & zoneArray(0).ID

  End Sub

  Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty
    Label1.Text = "WebPartZone1 index:  " & mgr.Zones.IndexOf(WebPartZone1)

  End Sub

  Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty

    Dim zoneCollection As WebPartZoneCollection = mgr.Zones
    Dim zone As WebPartZone
    For Each zone In zoneCollection
      If zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu Then
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.TitleBar
      Else
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu
      End If
    Next zone

  End Sub

End Class

在瀏覽器中載入頁面。 這三 WebPartZone 個區域會顯示其包含的伺服器控制項。 頁面底部附近有數個按鈕,每個按鈕都會示範使用 WebPartZoneCollection 物件的一些層面。 按一下每個按鈕,以查看其對區域及其內容的影響。 若要檢視與每個示範物件層面 WebPartZoneCollection 之按鈕相關聯的程式碼,請檢視上述部分類別檔案中參考的程式碼,作為此程式碼範例的第二個部分。

備註

類別 WebPartZoneCollection 包含存在於網頁元件頁面上的 WebPartZone 控制項集合。 它主要是由 WebPartManager 控制項用來追蹤和管理所有區域。 控制項 WebPartManager 會在其唯讀 Zones 屬性中,保留頁面上區域集合的參考。

當您想要同時對多個區域或其內容執行作業,或需要找出特定區域並檢查或變更其內容時,類別 WebPartZoneCollection 很有用。 雖然區域本身的集合是唯讀的,但您無法變更集合物件本身的大小,但您仍然可以取得集合內個別區域的參考,而且當您有個別區域物件的參考時,您可以變更其屬性值。

類別 WebPartZoneCollection 有兩個建構函式。 第一個 WebPartZoneCollection() 是 ,只會初始化新 WebPartZoneCollection 的空實例。 第二個是 WebPartZoneCollection(ICollection) ,藉由傳入現有的區域集合來初始化集合。

類別 WebPartZoneCollection 包含 Item[] 屬性。 這個屬性可讓您藉由傳入字串識別碼,在集合中尋找 WebPartZone 控制項。 區域是藉由比對字串識別碼與 ID 區域上的 WebPartZoneBase 屬性值來找到。

類別 WebPartZoneCollection 也有數個方法可用於檢查或存取其內容。 方法 Contains 會判斷集合中是否有特定區域。 方法 CopyTo 會將集合複製到 物件的陣列 WebPartZoneBase 。 最後, IndexOf 方法會傳回集合內特定區域的索引。

建構函式

WebPartZoneCollection()

初始化 WebPartZoneCollection 類別的空執行個體。

WebPartZoneCollection(ICollection)

透過傳入 WebPartZoneCollection 物件的集合,初始化 WebPartZone 類別的執行個體。

屬性

Count

取得 ReadOnlyCollectionBase 執行個體中包含的元素數目。

(繼承來源 ReadOnlyCollectionBase)
InnerList

取得包含於 ReadOnlyCollectionBase 執行個體中的項目清單。

(繼承來源 ReadOnlyCollectionBase)
Item[Int32]

透過傳入整數,傳回集合的特定成員。

Item[String]

透過傳入唯一識別項,傳回集合的特定成員。

方法

Contains(WebPartZoneBase)

傳回值,指出集合中是否存在特定區域。

CopyTo(WebPartZoneBase[], Int32)

複製集合至 WebPartZoneBase 物件的陣列。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回可逐一查看 ReadOnlyCollectionBase 執行個體的列舉值。

(繼承來源 ReadOnlyCollectionBase)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(WebPartZoneBase)

傳回集合特定成員的位置。

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

適用於

另請參閱