Breyta

Deila með


WebPartManager.CreateWebPart(Control) Method

Definition

Wraps a server control that is not a WebPart control with a GenericWebPart object, so that the control can have Web Parts functionality.

public:
 virtual System::Web::UI::WebControls::WebParts::GenericWebPart ^ CreateWebPart(System::Web::UI::Control ^ control);
public virtual System.Web.UI.WebControls.WebParts.GenericWebPart CreateWebPart (System.Web.UI.Control control);
abstract member CreateWebPart : System.Web.UI.Control -> System.Web.UI.WebControls.WebParts.GenericWebPart
override this.CreateWebPart : System.Web.UI.Control -> System.Web.UI.WebControls.WebParts.GenericWebPart
Public Overridable Function CreateWebPart (control As Control) As GenericWebPart

Parameters

control
Control

A server control that is not a WebPart control.

Returns

A GenericWebPart that wraps control and enables it to function as a true WebPart control.

Examples

The following code example demonstrates use of the CreateWebPart method. In the Button2_Click method, the CreateWebPart method is called to wrap a Calendar control with a GenericWebPart object before adding it to the zone.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Button2_Click(object sender, EventArgs e)
  {
    WebPartManager mgr = WebPartManager1;
    Calendar cal = new Calendar();
    cal.ID = "cal1";
    GenericWebPart calWebPart = mgr.CreateWebPart(cal);
    mgr.AddWebPart(calWebPart, WebPartZone1, 1);
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    if (WebPartZone1.WebParts.Count > 1)
    {
      WebPart cal = WebPartZone1.WebParts[1];
      if (cal.Controls[0].GetType().Name == "Calendar" 
        && cal != null)
        WebPartManager1.DeleteWebPart(cal);
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Adding a Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="WebPartManager1" 
        runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList  
            DisplayMode="HyperLink" 
            ID="BulletedList1" 
            runat="server"
            Title="My Links">
            <asp:ListItem Value="http://www.microsoft.com">
            Microsoft
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
            MSN
            </asp:ListItem>
            <asp:ListItem Value="http://www.contoso.com">
            Contoso Corp.
            </asp:ListItem>
          </asp:BulletedList>
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Delete Calendar" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Add Calendar" 
        OnClick="Button2_Click" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    Dim mgr As WebPartManager = WebPartManager1
    Dim cal As New Calendar()
    cal.ID = "cal1"
    Dim calWebPart As GenericWebPart = mgr.CreateWebPart(cal)
    mgr.AddWebPart(calWebPart, WebPartZone1, 1)
  End Sub

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)

    If WebPartZone1.WebParts.Count > 1 Then
      Dim cal As WebPart = WebPartZone1.WebParts(1)
      If cal.Controls(0).GetType().Name = "Calendar" AndAlso _
        cal IsNot Nothing Then
        WebPartManager1.DeleteWebPart(cal)
      End If
    End If
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Adding a Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="WebPartManager1" 
        runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList  
            DisplayMode="HyperLink" 
            ID="BulletedList1" 
            runat="server"
            Title="My Links">
            <asp:ListItem Value="http://www.microsoft.com">
            Microsoft
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
            MSN
            </asp:ListItem>
            <asp:ListItem Value="http://www.contoso.com">
            Contoso Corp.
            </asp:ListItem>
          </asp:BulletedList>
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Delete Calendar" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Add Calendar" 
        OnClick="Button2_Click" />
    </div>
    </form>
</body>
</html>

Remarks

The CreateWebPart method is the main mechanism for enabling server controls that are not WebPart controls to take on the same functionality as a WebPart control, and thus to participate fully in Web Parts applications. By using this method, developers vastly expand the number of server controls that they can use in a Web Parts application, because virtually any type of server control--standard ASP.NET controls, user controls, and custom controls--can be used.

The WebPartManager control also uses this method in two other scenarios to wrap server controls with a GenericWebPart object. When users add server controls to a page by using the ImportCatalogPart control, if the imported control is not a WebPart control, the CreateWebPart method is called. Also, when server controls are declared in persistence format within a WebPartZoneBase zone on a Web page, the CreateWebPart method is called for any controls that are not WebPart controls.

When you add a server control to a zone programmatically, a typical approach is to use the CreateWebPart method to wrap the control with a GenericWebPart object, and then call the AddWebPart method to add the control to the collection of all WebPart controls on the page, which is referenced by the WebParts property.

Notes to Inheritors

This method can be overridden to use a derived GenericWebPart class instead of the base class provided with the Web Parts control set.

Applies to

See also