語言

WebPartManager.GetCurrentWebPartManager(Page) 方法

定義

取得頁面上控制項目前實例 WebPartManager 的參考。

public:
 static System::Web::UI::WebControls::WebParts::WebPartManager ^ GetCurrentWebPartManager(System::Web::UI::Page ^ page);
public static System.Web.UI.WebControls.WebParts.WebPartManager GetCurrentWebPartManager(System.Web.UI.Page page);
static member GetCurrentWebPartManager : System.Web.UI.Page -> System.Web.UI.WebControls.WebParts.WebPartManager
Public Shared Function GetCurrentWebPartManager (page As Page) As WebPartManager

參數

page
Page

包含 .WebPartManager

傳回

A WebPartManager 則是參考頁面上目前控制項實例的 A。

例外狀況

page 是 null。

範例

以下程式碼範例示範如何使用此 GetCurrentWebPartManager 方法。 範例分為兩部分:自訂伺服器控制,以及承載控制控制的網頁。

自訂 Label 控制項使用 GetCurrentWebPartManager 該方法取得當前頁面控制項的 ID WebPartManager ,然後顯示該 ID。

namespace Samples.AspNet.CS.Controls
{
  using System;
  using System.Web;
  using System.Web.Security;
  using System.Security.Permissions;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class MyManagerIDLabel : Label
  {

    protected override void OnPreRender(EventArgs e)
    {
      EnsureChildControls();

      this.Text = 
        WebPartManager.GetCurrentWebPartManager(Page).ID;
    }
  }
}
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class MyManagerIDLabel

    Inherits Label

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)

      EnsureChildControls()
      Me.Text = _
        WebPartManager.GetCurrentWebPartManager(Page).ID

    End Sub

  End Class

End Namespace

以下程式碼範例提供了在區域 WebPartZone 中託管控制的網頁。

<%@ Page Language="C#" %>
<%@ Register 
    Namespace="Samples.AspNet.CS.Controls" 
    TagPrefix="aspSample"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="WebPartManager1" runat="server">
      </asp:WebPartManager>
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:MyManagerIDLabel ID="mgrID" runat="server" 
            Title="Manager ID Label" 
            Description="Displays the ID of the current WebPartManger."/>
        </ZoneTemplate>
      </asp:WebPartZone>
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register 
    Namespace="Samples.AspNet.VB.Controls" 
    TagPrefix="aspSample"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="WebPartManager1" runat="server">
      </asp:WebPartManager>
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:MyManagerIDLabel ID="mgrID" runat="server" 
            Title="Manager ID Label" 
            Description="Displays the ID of the current WebPartManger."/>
        </ZoneTemplate>
      </asp:WebPartZone>
    </div>
    </form>
</body>
</html>

在瀏覽器載入頁面後,請注意自訂控制項中顯示WebPartManager了目前Label控制項的 ID。

備註

此 GetCurrentWebPartManager 方法在想要取得當前 WebPartManager 控制項參考的情境中非常有用。 一個常見的情況是,如果你在撰寫一個自訂控制項,在開發過程中無法知道該控制項在頁面上的 ID WebPartManager 是什麼。

Note

這個 GetCurrentWebPartManager 方法是靜態的,所以你可以直接呼叫,不需要控制項實例 WebPartManager 。

Web Parts 控制集中的某些控制項,如 WebPart controls,具有 WebPartManager 可檢索當前 WebPartManager 控制項參考的屬性。 因此,在使用此類控制項時,應使用此屬性來取得參考資料。

如果你是在已知控制項 ID WebPartManager 的情境中編碼,例如在網頁內行撰寫程式碼,直接參考 WebPartManager 控制項的 ID 是最簡單且最有效率的做法。

適用於

另請參閱