WebPartManager.GetCurrentWebPartManager(Page) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索对页上的 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 的实例的网页。
返回
一个 WebPartManager,引用该控件在页上的当前实例。
例外
page
为 null
。
示例
下面的代码示例演示如何使用 GetCurrentWebPartManager 方法。 该示例有两个部分:自定义服务器控件和承载控件的网页。
自定义 Label 控件使用 GetCurrentWebPartManager 该方法检索当前页上控件的 WebPartManager ID,然后显示 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 控件的 ID 显示在自定义 Label 控件中。
注解
此方法 GetCurrentWebPartManager 在要检索对当前 WebPartManager 控件的引用的上下文中非常有用。 一种常见的情况是,如果要编写一个自定义控件,该控件在开发过程中无法知道其页面上控件的 WebPartManager ID。
备注
该方法 GetCurrentWebPartManager 是静态的,因此可以直接调用它,而无需控件的 WebPartManager 实例。
Web 部件控件集中的某些控件(如WebPart控件)具有可WebPartManager检索对当前WebPartManager控件的引用的属性。 因此,使用此类控件时,应使用此属性检索引用。
如果要在知道控件 ID 的 WebPartManager 上下文中进行编码,例如在网页内内内编写代码,则使用控件 ID 直接 WebPartManager 引用控件是最简单的且最有效的。