WebPartManager.Zones 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會看到網頁上所有 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
屬性值
A WebPartZoneCollection 指涉一組 WebPartZoneBase 區域。
- 屬性
範例
以下程式碼範例示範如何以程式化方式使用該 Zones 屬性來存取各個 WebPartZoneBase 區域控制。 請注意,在網頁的宣告式標記中,有兩個 <asp:webpartzone> 元素,每個元素都包含一個伺服器控制。 在頁面的該 <script> 區塊中,程式碼使用該 Zones 屬性來存取各個區域,列出所有區域 ID,然後更改第二個區域的背景顏色。
<%@ 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.config 檔案,與此程式碼範例的網頁目錄相同。 在該 <system.web> 區塊中,請確保有一個 <webParts> 屬性 enableExport 設定為 true的元素,就像接下來的標記一樣。
<webParts enableExport="true">
...
</webParts>
當你將頁面載入瀏覽器後,點擊 「列表區域 ID」 按鈕,程式碼會利用該 Zones 屬性列出集合中所有區域的 ID。 如果你點擊 「變更區域回退顏色 」按鈕,程式碼會改變第二個區域的背景色。
備註
控制單位會利用Zones該WebPartManager屬性來追蹤網頁上的區域。WebPartZoneBase 請注意,該物業並非涵蓋所有類型的區域;它僅參考源自該 WebPartZoneBase 類別的區域,包括 WebPartZone 區域。
雖然該屬性所參考的集合是唯讀,但你可以用它來存取集合中的個別物件,並以程式方式處理它們。