WebPartManager.WebParts 屬性

定義

取得網頁上 WebPart 控制項所追蹤之所有 WebPartManager 控制項的參考。

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

屬性值

WebPartCollection,包含 WebPart 控制集的參考。

屬性

範例

下列程式碼範例示範如何以程式設計方式使用 WebParts 屬性來存取個別 WebPart 控制項。 請注意,在網頁的宣告式標記中,元素內 <asp:webpartzone> 有兩個標準 ASP.NET 伺服器控制項。 雖然這些不會繼承自 WebPart 類別,因為它們位於區域中,所以會在執行時間以 GenericWebPart 物件包裝,因此會包含在 屬性所參考的 WebParts 集合中。 您也可以在此區域中新增自訂 WebPart 控制項、使用者控制項或自訂伺服器控制項,而且處理方式相同。

<%@ 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)
  {
    Label1.Text = "WebPart count:  " +
      WebPartManager1.WebParts.Count.ToString();
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
    WebPartManager1.WebParts["Calendar1"].ChromeType = 
      PartChromeType.BorderOnly;
  }
</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>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="WebPart Count" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Hide Calendar Title" 
        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)
    
    Label1.Text = "WebPart count:  " & _
      WebPartManager1.WebParts.Count.ToString()
  End Sub

  Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    WebPartManager1.WebParts("Calendar1").ChromeType = _
      PartChromeType.BorderOnly

  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>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="WebPart Count" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Hide Calendar Title" 
        OnClick="Button2_Click" />  
      <br />
      <asp:Label ID="Label1" runat="server" text="" />
    </div>
    </form>
</body>
</html>

請注意,若要讓程式碼範例能夠運作,您必須在 Web.config 檔案中新增設定,以啟用匯出網頁元件描述檔案。 請確定您在與此程式碼範例網頁相同的目錄中有一個Web.config檔案。 在 區 <system.web> 段中,請確定有屬性 <webParts> 設定為 enableExporttrue 的專案,如下列標記所示。

<webParts enableExport="true">

...

</webParts>

將頁面載入瀏覽器之後,如果您按一下 [WebPart Count] 按鈕,程式碼會使用 WebParts 屬性傳回集合中的控制項計數。 如果您按一下 [ 隱藏行事曆標題 ] 按鈕,程式碼會變更行事曆,使其只以框線呈現,而不是標題。

備註

控制項 WebParts 會使用 WebPartManager 屬性來追蹤頁面上區域內 WebPartZoneBase 包含的所有 WebPart 控制項。 雖然集合是唯讀的,但您可以透過集合存取個別 WebPart 控制項,並以程式設計方式變更這些控制項。

注意

控制項可以 WebPart 放置於區域外部 WebPartZone 的頁面,這會導致控制項未由控制項追蹤 WebPartManager 或在其 WebParts 集合中參考。 不過,在區域外部使用 WebPart 控制項很少,因為它會失去其網頁元件功能,並做為一般伺服器控制項。

任何可以放在區域中的控制項類型,無論是自訂 WebPart 控制項、標準 ASP.NET 控制項、使用者控制項或自訂伺服器控制項,都可以在執行時間視為 WebPart 控制項。 當不是 WebPart 控制項的控制項放在 WebPartZone 區域中時,在執行時間 ASP.NET 將控制項包裝 GenericWebPart 成 物件,讓控制項可以做為真正的 WebPart 控制項。 因此,不論 WebParts 控制項是否衍生自 WebPart 類別, WebPartManager 控制項都可以使用 屬性來追蹤任何類型的伺服器控制項。

適用於