The following code example demonstrates how to use the GetCurrentWebPartManager method. The example has two parts: a custom server control, and a Web page that hosts the control.
The custom Label control uses the GetCurrentWebPartManager method to retrieve the ID of the WebPartManager control on the current page, and then displays the ID.
Imports System
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
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;
}
}
}
The following code example provides the Web page that hosts the control in a WebPartZone zone.
<%@ 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 >
<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>
<%@ 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 >
<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>
After you load the page in a browser, notice that the ID of the current WebPartManager control is displayed within the custom Label control.