Export (0) Print
Expand All

WebPartManager.Zones Property

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)

[BrowsableAttribute(false)]
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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <div>
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            DisplayMode="HyperLink" 
            ID="BulletedList1" 
            runat="server"
            Title="My Links"
            ExportMode="All">
            <asp:ListItem Value="http://www.microsoft.com">
            Microsoft
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
            MSN
            </asp:ListItem>
            <asp:ListItem Value="http://www.contoso.com">
            Contoso Corp.
            </asp:ListItem>
          </asp:BulletedList>
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:WebPartZone ID="WebPartZone2" runat="server">
        <ZoneTemplate>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />        
        </ZoneTemplate>
      </asp:WebPartZone>
      <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="" />
    </div>
    </form>
</body>
</html>

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">

...

</webParts>

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

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
Show:
© 2015 Microsoft