Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

WebPartManager.GetGenericWebPart Method

Note: This method is new in the .NET Framework version 2.0.

Gets a reference to the instance of the GenericWebPart control that contains a server control.

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

public GenericWebPart GetGenericWebPart (
	Control control
)
public GenericWebPart GetGenericWebPart (
	Control control
)
public function GetGenericWebPart (
	control : Control
) : GenericWebPart

Parameters

control

A server control that exists in a WebPartZoneBase and is wrapped as a child control of a GenericWebPart at run time.

Return Value

A GenericWebPart that wraps control as a child control. The method returns a null reference (Nothing in Visual Basic) if control is not contained in a GenericWebPart.

Exception typeCondition

ArgumentNullException

control is a null reference (Nothing in Visual Basic).

In general, there are two categories of controls that developers place in WebPartZoneBase zones to participate in Web Parts applications: WebPart controls, which inherit from the WebPart base class, and other server controls, which can be standard ASP.NET controls, custom controls, or user controls. When any of these controls is placed in a WebPartZoneBase zone, it takes on the functionality of a WebPart control. A WebPart control has this functionality inherently, but the other kinds of server controls do not. To enable the other server controls to act as WebPart controls when they are placed in a WebPartZoneBase zone, ASP.NET wraps them with a GenericWebPart control. Because the GenericWebPart control inherits directly from the WebPart class, it provides its child controls with true Web Parts features.

Often at run time, page developers might want to get a reference to the GenericWebPart control that contains one of the server controls in a zone. The GetGenericWebPart method enables them to retrieve a reference to the GenericWebPart control.

The following code example demonstrates the use of the GetGenericWebPart method. The code example contains a Calendar control declared within a WebPartZone zone. The Button1_Click method first prints the ID of the Calendar control to a label, and then uses the GetGenericWebPart method to retrieve a reference to the GenericWebPart control that wraps the calendar. The ID of the GenericWebPart control, and the ID of its child control (which is the Calendar control), are both printed to a second label.

<%@ Page Language="C#" %>

<script runat="server">
 
  protected void Button1_Click(object sender, EventArgs e)
  {
    Label1.Text = "<h2>Server Control</h2>";
    Label1.Text += "Server Control ID:  " + Calendar1.ID;
    Label2.Text = "<h2>GenericWebPart Control</h2>";
    GenericWebPart part = mgr.GetGenericWebPart(Calendar1);
    if (part != null)
    {
      Label2.Text +=
        "GenericWebPart ID:  " + part.ID + "<br />";
      Label2.Text +=
        "Underlying Control ID: " + part.ChildControl.ID;
    }
  }

</script>

<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="mgr" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:Calendar ID="Calendar1" runat="server"
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Get GenericWebPart"
        OnClick="Button1_Click" />
      <hr />
      <asp:Label ID="Label1" runat="server" Text="" />
      <br />
      <asp:Label ID="Label2" runat="server" Text="" />
    </div>
    </form>
</body>
</html>

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft