WebPartManager Methods


.NET Framework Class Library
WebPartManager..::.GetCurrentWebPartManager Method

Retrieves a reference to the current instance of the WebPartManager control on a page.

Namespace:  System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic (Declaration)
Public Shared Function GetCurrentWebPartManager ( _
    page As Page _
) As WebPartManager
Visual Basic (Usage)
Dim page As Page
Dim returnValue As WebPartManager

returnValue = WebPartManager.GetCurrentWebPartManager(page)
C#
public static WebPartManager GetCurrentWebPartManager(
    Page page
)
Visual C++
public:
static WebPartManager^ GetCurrentWebPartManager(
    Page^ page
)
JScript
public static function GetCurrentWebPartManager(
    page : Page
) : WebPartManager

Parameters

page
Type: System.Web.UI..::.Page
The Web page that contains an instance of the WebPartManager.

Return Value

Type: System.Web.UI.WebControls.WebParts..::.WebPartManager
A WebPartManager that references the current instance of the control on a page.
Exceptions

ExceptionCondition
ArgumentNullException

page is nullNothingnullptra null reference (Nothing in Visual Basic).

Remarks

The GetCurrentWebPartManager method is useful in contexts where you want to retrieve a reference to the current WebPartManager control. A common scenario where this would occur is if you are writing a custom control that cannot know during development what the ID of the WebPartManager control on its page will be.

NoteNote:

The GetCurrentWebPartManager method is static, so you can call it directly without needing an instance of a WebPartManager control.

Some controls in the Web Parts control set, such as WebPart controls, have a WebPartManager property that can retrieve a reference to the current WebPartManager control. Hence, when working with such controls, you should use this property to retrieve a reference.

If you are coding in a context where you know the ID of the WebPartManager control, such as writing code inline within a Web page, it is simplest and most efficient to refer directly to the WebPartManager control by using its ID.

Examples

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.

Visual Basic
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
C#
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.

Visual Basic
<%@ 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>
C#
<%@ 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.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Page view tracker