WebPartZoneCollection Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Contains a read-only collection of WebPartZone controls.

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


public sealed class WebPartZoneCollection : ReadOnlyCollectionBase


Initializes an empty instance of the WebPartZoneCollection class.


Initializes an instance of the WebPartZoneCollection class by passing in a collection of WebPartZone objects.


Gets the number of elements contained in the ReadOnlyCollectionBase instance.(Inherited from ReadOnlyCollectionBase.)


Returns a specific member of the collection by passing in an integer.


Returns a specific member of the collection by passing in a unique identifier.


Returns a value indicating whether a particular zone exists in the collection.

System_CAPS_pubmethodCopyTo(WebPartZoneBase[], Int32)

Copies the collection to an array of WebPartZoneBase objects.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Returns an enumerator that iterates through the ReadOnlyCollectionBase instance.(Inherited from ReadOnlyCollectionBase.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Returns the position of a particular member of the collection.


Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

Copies the entire ReadOnlyCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.(Inherited from ReadOnlyCollectionBase.)


Gets a value indicating whether access to a ReadOnlyCollectionBase object is synchronized (thread safe).(Inherited from ReadOnlyCollectionBase.)


Gets an object that can be used to synchronize access to a ReadOnlyCollectionBase object.(Inherited from ReadOnlyCollectionBase.)


Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)


Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)


Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)


Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

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="C#" 
    Inherits="webpartzonecollection_overview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<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" 
            WebPartZone1 Contents
          <asp:WebPartZone ID="WebPartZone1" 
            runat="server" Width="230">
                Title="Favorite Links">
                <asp:ListItem Value="http://www.asp.net">
                <asp:ListItem Value="http://www.msn.com">
          <asp:Label ID="lblZone2" runat="server" 
            WebPartZone2 Contents
          <asp:WebPartZone ID="WebPartZone2" 
            runat="server" Width="230">
              <asp:Calendar ID="Calendar1" 
                Title="My Calendar" 
                CatalogIconImageUrl="Mine.gif" />
        <asp:TableCell VerticalAlign="top"> 
          <asp:Label ID="lblZone3" runat="server" 
            WebPartZone3 Contents
          <asp:WebPartZone ID="WebPartZone3" runat="server">
              <asp:Table runat="server" ID="table2" 
                Title="Employee Extensions">
                  <asp:TableHeaderCell Scope="Column">
                    Employee Name</asp:TableHeaderCell>
                  <asp:TableHeaderCell Scope="Column">
                  <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" 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 HorizontalAlign="right" 
          Width="200" VerticalAlign="top">
          <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.

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;
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu;

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.

.NET Framework
Available since 2.0

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

Return to top