Export (0) Print
Expand All

WebPartZoneCollection Class

Note: This class is new in the .NET Framework version 2.0.

Contains a read-only collection of WebPartZone controls.

Namespace: System.Web.UI.WebControls.WebParts
Assembly: System.Web (in system.web.dll)

Public NotInheritable Class WebPartZoneCollection
	Inherits ReadOnlyCollectionBase
Dim instance As WebPartZoneCollection

public final class WebPartZoneCollection extends ReadOnlyCollectionBase
public final class WebPartZoneCollection extends ReadOnlyCollectionBase

The WebPartZoneCollection class contains the collection of WebPartZone controls that exist on a Web Parts page. It is used primarily by the WebPartManager control to track and manage all the zones. The WebPartManager control keeps a reference to the collection of zones on a page in its read-only Zones property.

The WebPartZoneCollection class is useful when you want to carry out operations on multiple zones or their contents at the same time, or when you need to locate a particular zone and examine or change its contents. Although the collection of zones itself is read-only and you cannot change the size of the collection object itself, you can still get references to the individual zones within the collection, and when you have a reference to an individual zone object, you can change its property values.

The WebPartZoneCollection class has two constructors. The first one, WebPartZoneCollection, simply initializes an empty instance of a new WebPartZoneCollection. The second one, WebPartZoneCollection(ICollection), initializes a collection by passing in an existing collection of zones.

The WebPartZoneCollection class contains an Item property. This property allows you to locate a WebPartZone control within the collection by passing in a string identifier. The zone is located by matching the string identifier to the value of the ID property on a WebPartZoneBase zone.

The WebPartZoneCollection class also has several methods that are useful for examining or accessing its contents. The Contains method determines whether a particular zone exists in the collection. The CopyTo method copies the collection to an array of WebPartZoneBase objects. Finally, the IndexOf method returns the index of a particular zone within the collection.

The following code example demonstrates the use of the WebPartZoneCollection class. The code example contains three parts: two sections of code, and an explanation of how to run the example.

The first part of the code example is a Web page that contains three WebPartZone controls, each of which contains a single server control. Although these server controls are all standard ASP.NET controls, they can act as WebPart controls at run time because they are placed within a WebPartZone zone.

<%@ Page Language="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>
    <form id="form1" runat="server">
    <asp:WebPartManager ID="mgr" runat="server" />
    <asp:Table ID="Table1" runat="server">
          <asp:Label ID="lblZone1" runat="server" Font-Bold="true">
            WebPartZone1 Contents
          <asp:WebPartZone ID="WebPartZone1" runat="server" Width="230">
                Title="Favorite Links" CatalogIconImageUrl="Mine.gif">
                <asp:ListItem Value="http://msdn.microsoft.com">
                <asp:ListItem Value="http://www.asp.net">
                <asp:ListItem Value="http://www.msn.com">
          <asp:Label ID="lblZone2" runat="server" Font-Bold="true">
            WebPartZone2 Contents
          <asp:WebPartZone ID="WebPartZone2" runat="server" Width="230">
              <asp:Calendar ID="Calendar1" runat="server" 
                Title="My Calendar" />
        <asp:TableCell VerticalAlign="top"> 
          <asp:Label ID="lblZone3" runat="server" Font-Bold="true">
            WebPartZone3 Contents
          <asp:WebPartZone ID="WebPartZone3" runat="server">
              <asp:Table runat="server" ID="table2" 
                Title="Employee Extensions">
                  <asp:TableHeaderCell>Employee Name</asp:TableHeaderCell>
                  <asp:TableCell>Alberts, Amy</asp:TableCell>
                  <asp:TableCell>Hanif, Karim</asp:TableCell>
                  <asp:TableCell>Penor, Lori</asp:TableCell>
    <hr />
    <asp:Table ID="Table3" runat="server">
          <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 HorizontalAlign="right" Width="200" 
          <asp:Label ID="Label1" runat="server" Font-Bold="true" />

The second part of the code example is the partial class that contains the code to demonstrate usage of the WebPartZoneCollection class. This code handles the events for the Web page.

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
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu
      End If
    Next zone

  End Sub

End Class

Load the page in a browser. The three WebPartZone zones are visible with their contained server controls. Near the bottom of the page are several buttons, each of which demonstrates some aspect of working with the WebPartZoneCollection object. Click each button to see its effect on the zones and their contents. To view the code associated with each button that demonstrates some aspect of the WebPartZoneCollection object, view the code in the partial class file referenced above as the second part of this code example.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

© 2014 Microsoft