WebPartManager.Zones Property


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

Gets a reference to a collection of all the WebPartZoneBase zones on a Web page.

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

public WebPartZoneCollection Zones { get; }

The Zones property is used by the WebPartManager control to track the WebPartZoneBase zones on a Web page. Note that the property does not reference all types of zones; it references only zones that derive from the WebPartZoneBase class, including WebPartZone zones.

Although the collection referenced by the property is read-only, you can use it to access the individual objects in the collection and work with them programmatically.

The following code example demonstrates how to use the Zones property programmatically to access individual WebPartZoneBase zone controls. Notice that in the declarative markup for the Web page, there are two <asp:webpartzone> elements, each containing a server control. In the <script> section of the page, the code uses the Zones property to access the individual zones, listing all the zone IDs and then changing the background color on the second zone.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
    foreach (WebPartZone zone in WebPartManager1.Zones)
      Label1.Text += zone.ID + "<br />";

  protected void Button2_Click(object sender, EventArgs e)
    WebPartManager1.Zones["WebPartZone2"].BackColor = System.Drawing.Color.LightBlue;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
            Title="My Links"
            <asp:ListItem Value="http://www.microsoft.com">
            <asp:ListItem Value="http://www.msn.com">
            <asp:ListItem Value="http://www.contoso.com">
            Contoso Corp.
      <asp:WebPartZone ID="WebPartZone2" runat="server">
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />        
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="List Zone IDs" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Change Zone BackColor" 
        OnClick="Button2_Click" />  
      <br />
      <asp:Label ID="Label1" runat="server" text="" />

Note that for the code example to work, you must add a setting in the Web.config file to enable exporting Web Parts description files. Ensure that you have a Web.config file in the same directory as the Web page for this code example. Within the <system.web> section, make sure there is a <webParts> element with an enableExport attribute set to true, as in the following markup.

<webParts enableExport="true">



After you load the page into a browser, if you click the List Zone IDs button, the code uses the Zones property to list the IDs of all zones in the collection. If you click the Change Zone BackColor button, the code changes the background color of the second zone.

.NET Framework
Available since 2.0
Return to top