WebPartManager.Zones 属性

定义

获取对网页上所有 WebPartZoneBase 区域的集合的引用。Gets a reference to a collection of all the WebPartZoneBase zones on a Web page.

public:
 property System::Web::UI::WebControls::WebParts::WebPartZoneCollection ^ Zones { System::Web::UI::WebControls::WebParts::WebPartZoneCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.WebParts.WebPartZoneCollection Zones { get; }
member this.Zones : System.Web.UI.WebControls.WebParts.WebPartZoneCollection
Public ReadOnly Property Zones As WebPartZoneCollection

属性值

引用一组 WebPartZoneCollection 区域的 WebPartZoneBaseA WebPartZoneCollection that references a set of WebPartZoneBase zones.

属性

示例

下面的代码示例演示如何以编程方式Zones使用属性访问单个WebPartZoneBase区域控件。The following code example demonstrates how to use the Zones property programmatically to access individual WebPartZoneBase zone controls. 请注意,在网页的声明性标记中,有两个<asp:webpartzone>元素,每个元素都包含一个服务器控件。Notice that in the declarative markup for the Web page, there are two <asp:webpartzone> elements, each containing a server control. 在该页的Zones <script>节中,代码使用属性访问各个区域,列出所有区域 id,然后更改第二个区域的背景色。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>
<%@ Page Language="vb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    Dim zone As WebPartZone
    For Each zone In WebPartManager1.Zones
      Label1.Text += zone.ID & "<br />"
    Next
  End Sub

  Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    WebPartManager1.Zones("WebPartZone2").BackColor = _
      System.Drawing.Color.LightBlue
  End Sub
</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>

请注意,若要使代码示例正常运行,必须在 web.config 文件中添加一个设置,以便导出 Web 部件说明文件。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. 确保在此代码示例的网页所在的同一目录中有一个 web.config 文件。Ensure that you have a Web.config file in the same directory as the Web page for this code example. 在部分中,请确保有一个<webParts>元素的enableExport属性设置为true,如以下标记所示。 <system.web>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>

将页面加载到浏览器中后,如果单击 "列出区域 id " 按钮,代码将使用Zones属性列出集合中所有区域的 id。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.

注解

控件使用WebPartManager属性来跟踪网页上的WebPartZoneBase区域。 ZonesThe Zones property is used by the WebPartManager control to track the WebPartZoneBase zones on a Web page. 请注意,属性不引用所有类型的区域;它仅引用派生自WebPartZoneBase类的区域,包括WebPartZone区域。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.

适用于