WebPartZoneCollection クラス
アセンブリ: System.Web (system.web.dll 内)


WebPartZoneCollection クラスには、Web パーツ ページに存在する WebPartZone コントロールのコレクションが含まれます。主に WebPartManager コントロールによって、すべてのゾーンの追跡および管理に使用されます。WebPartManager コントロールは、ページ上のゾーンのコレクションへの参照を、その読み取り専用の Zones プロパティに保持します。
WebPartZoneCollection クラスは、複数のゾーンまたはその内容に対して同時に操作を実行する場合、または特定のゾーンを検索してその内容を確認または変更する場合に便利です。ゾーン自体のコレクションは読み取り専用で、コレクション オブジェクト自体のサイズは変更できませんが、コレクション内の個別のゾーンへの参照を取得することはできます。また、個別のゾーン オブジェクトへの参照を保持している場合はそのプロパティ値を変更できます。
WebPartZoneCollection クラスには、2 つのコンストラクタがあります。1 つ目の WebPartZoneCollection は、新しい WebPartZoneCollection の空のインスタンスを初期化するだけです。2 つ目の WebPartZoneCollection(ICollection) は、ゾーンの既存のコレクションを渡すことによってコレクションを初期化します。
WebPartZoneCollection クラスには、Item プロパティが含まれています。このプロパティでは、文字列識別子を渡すことによってコレクション内の WebPartZone コントロールを検索できます。ゾーンを検索するには、文字列識別子と WebPartZoneBase ゾーンの ID プロパティの値が照合されます。
WebPartZoneCollection クラスには、その内容の確認またはアクセスに便利なメソッドも複数あります。Contains メソッドは、特定のゾーンがコレクションに存在するかどうかを判断します。CopyTo メソッドは、コレクションを WebPartZoneBase オブジェクトの配列にコピーします。最後に、IndexOf メソッドは、コレクション内の特定のゾーンのインデックスを返します。

WebPartZoneCollection クラスの使用方法を示すコード例を次に示します。コード例は、3 つの部分で構成されます。2 つがコード セクション、残りが例の実行方法の説明です。
コード例の最初の部分は、それぞれにサーバー コントロールを 1 つ含む 3 つの WebPartZone コントロールがある Web ページです。これらのサーバー コントロールはすべて標準の ASP.NET コントロールですが、WebPartZone ゾーン内に配置されているので、実行時には WebPart コントロールとして機能できます。
<%@ 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 > <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"> WebPartZone1 Contents </asp:Label> <asp:WebPartZone ID="WebPartZone1" runat="server" Width="230"> <ZoneTemplate> <asp:BulletedList ID="BulletedList1" Runat="server" DisplayMode="HyperLink" Title="Favorite Links" CatalogIconImageUrl="Mine.gif"> <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"> WebPartZone2 Contents </asp:Label> </div> <asp:WebPartZone ID="WebPartZone2" runat="server" Width="230"> <ZoneTemplate> <asp:Calendar ID="Calendar1" runat="server" Title="My Calendar" /> </ZoneTemplate> </asp:WebPartZone> </asp:TableCell> <asp:TableCell VerticalAlign="top"> <asp:Label ID="lblZone3" runat="server" Font-Bold="true"> WebPartZone3 Contents </asp:Label> <asp:WebPartZone ID="WebPartZone3" runat="server"> <ZoneTemplate> <asp:Table runat="server" ID="table2" Title="Employee Extensions"> <asp:TableHeaderRow> <asp:TableHeaderCell>Employee Name</asp:TableHeaderCell> <asp:TableHeaderCell>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" Text="Total Zone Count" OnClick="Button1_Click" /> <br /> <asp:Button ID="Button2" runat="server" Width ="200" Text="Coll. Contains WebPartZone2" OnClick="Button2_Click" /> <br /> <asp:Button ID="Button3" runat="server" Width ="200" Text="Zone Names from Array" OnClick="Button3_Click" /> <br /> <asp:Button ID="Button4" runat="server" Width ="200" Text="WebPartZone1 Index" OnClick="Button4_Click" /> <br /> <asp:Button ID="Button5" runat="server" Width ="200" Text="Toggle Verb Render Mode" OnClick="Button5_Click" /> </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="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 > <head runat="server"> <title>WebPartZoneCollection Demo</title> </head> <body> <form id="form1" runat="server"> <div> <asp:WebPartManager ID="mgr" runat="server" /> <asp:Table runat="server"> <asp:TableRow> <asp:TableCell> <asp:Label ID="lblZone1" runat="server" Font-Bold="true"> WebPartZone1 Contents </asp:Label> <asp:WebPartZone ID="WebPartZone1" runat="server" Width="230"> <ZoneTemplate> <asp:BulletedList ID="BulletedList1" Runat="server" DisplayMode="HyperLink" Title="Favorite Links" CatalogIconImageUrl="Mine.gif"> <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"> WebPartZone2 Contents </asp:Label> </div> <asp:WebPartZone ID="WebPartZone2" runat="server" Width="230"> <ZoneTemplate> <asp:Calendar ID="Calendar1" runat="server" Title="My Calendar" /> </ZoneTemplate> </asp:WebPartZone> </asp:TableCell> <asp:TableCell VerticalAlign="top"> <asp:Label ID="lblZone3" runat="server" Font-Bold="true"> WebPartZone3 Contents </asp:Label> <asp:WebPartZone ID="WebPartZone3" runat="server"> <ZoneTemplate> <asp:Table runat="server" ID="table1" Title="Employee Extensions"> <asp:TableHeaderRow> <asp:TableHeaderCell>Employee Name</asp:TableHeaderCell> <asp:TableHeaderCell>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 runat="server"> <asp:TableRow> <asp:TableCell> <asp:Button ID="Button1" runat="server" Width ="200" Text="Total Zone Count" OnClick="Button1_Click" /> <br /> <asp:Button ID="Button2" runat="server" Width ="200" Text="Coll. Contains WebPartZone2" OnClick="Button2_Click" /> <br /> <asp:Button ID="Button3" runat="server" Width ="200" Text="Zone Names from Array" OnClick="Button3_Click" /> <br /> <asp:Button ID="Button4" runat="server" Width ="200" Text="WebPartZone1 Index" OnClick="Button4_Click" /> <br /> <asp:Button ID="Button5" runat="server" Width ="200" Text="Toggle Verb Render Mode" OnClick="Button5_Click" /> </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>
コード例の 2 番目の部分は、WebPartZoneCollection クラスの使用を示すコードを含む部分クラスです。このコードは、Web ページのイベントを処理します。
Imports System 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
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; } } }
ブラウザでページを読み込みます。3 つの WebPartZone ゾーンが、格納しているサーバー コントロールと共に表示されます。ページの下部には、複数のボタンがあり、それぞれが WebPartZoneCollection オブジェクトを使用する動作をデモンストレーションします。各ボタンをクリックして、ゾーンとその内容に対する効果を確認します。WebPartZoneCollection オブジェクトのいくつかの側面を示す各ボタンに関連付けられたコードを表示するには、上記のこのコード例の 2 番目の部分として参照される部分クラス ファイルのコードを表示します。


System.Collections.ReadOnlyCollectionBase
System.Web.UI.WebControls.WebParts.WebPartZoneCollection


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- WebPartZoneCollection クラスのページへのリンク