WebPartManager.Zones 屬性

定義

取得網頁上所有 WebPartZoneBase 區域之集合的參考。

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; }
[<System.ComponentModel.Browsable(false)>]
member this.Zones : System.Web.UI.WebControls.WebParts.WebPartZoneCollection
Public ReadOnly Property Zones As WebPartZoneCollection

屬性值

WebPartZoneCollection

WebPartZoneCollection,參考 WebPartZoneBase 區域的集合。

屬性

範例

下列程式碼範例示範如何以程式設計方式使用 Zones 屬性來存取個別 WebPartZoneBase 區域控制項。 請注意,在網頁的宣告式標記中,有兩 <asp:webpartzone> 個元素,每個元素都包含伺服器控制項。 <script>在頁面的 區段中,程式碼會使用 Zones 屬性來存取個別區域、列出所有區域識別碼,然後變更第二個區域的背景色彩。

<%@ 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 組件描述檔。 請確定您在與此程式碼範例網頁相同的目錄中有一個Web.config檔案。 在 區 <system.web> 段中,請確定有屬性 <webParts> 設定為 enableExport true 的專案,如下列標記所示。

<webParts enableExport="true">

...

</webParts>

將頁面載入瀏覽器之後,如果您按一下 [列出區域識別碼 ] 按鈕,程式碼會使用 Zones 屬性來列出集合中所有區域的識別碼。 如果您按一下 [ 變更區域 BackColor ] 按鈕,程式碼會變更第二個區域的背景色彩。

備註

控制項 Zones 會使用 WebPartManager 屬性來追蹤 WebPartZoneBase 網頁上的區域。 請注意,屬性不會參考所有類型的區域;它只會參考衍生自 WebPartZoneBase 類別的區域,包括 WebPartZone 區域。

雖然 屬性所參考的集合是唯讀的,但您可以使用它來存取集合中的個別物件,並以程式設計方式使用這些物件。

適用於