This documentation is archived and is not being maintained.

WebPartManager.AddWebPart Method

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

Provides the standard programmatic method for adding WebPart controls to a Web page.

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

public WebPart AddWebPart (
	WebPart webPart,
	WebPartZoneBase zone,
	int zoneIndex
public WebPart AddWebPart (
	WebPart webPart, 
	WebPartZoneBase zone, 
	int zoneIndex
public function AddWebPart (
	webPart : WebPart, 
	zone : WebPartZoneBase, 
	zoneIndex : int
) : WebPart



The WebPart (or server or user control) to be added to a Web page or opened on a page.


The WebPartZoneBase that webPart is being added to.


An integer that represents the ordinal position that webPart occupies in zone, relative to other controls in zone.

Return Value

A WebPart control that was added to the page.

Exception typeCondition


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

- or -

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


zone is not registered in the WebPartManager control's collection of zones.

- or -

webPart is already in zone.


The value of zoneIndex is less than zero.

The AddWebPart method is used both to add new dynamic WebPart controls to a page, and to reopen static or dynamic controls that have previously been closed on a page. When the method is called to add a new control, it actually creates a copy of the control referenced in the webPart parameter. A new ID is generated for the copy of the control, so developers should reference the WebPart control returned from the method to get the new ID value. When the method is called to reopen a previously closed control, it returns a direct reference to the control referenced by the webPart parameter.


You should always use the AddWebPart method, rather than the Add method of the collection of controls referenced by the WebPartManager.Controls property, to add WebPart controls programmatically to the page, because using the Add method throws an exception. To add a control that is not a WebPart control (in other words, a server control that will be wrapped with a GenericWebPart control at run time), you should first call the CreateWebPart method to create the control, and then call the AddWebPart method to add the control. For a demonstration of this approach, see the Example section.

The following code example demonstrates use of the AddWebPart method to add a server control programmatically to a page. The page markup contains an empty <asp:webpartzone> element, and an <asp:webpartmanager> element. The first time the Add Calendar button is clicked, the code in the event handler creates a Calendar control, and adds it to a zone as a GenericWebPart object, calling the AddWebPart method.

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

<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)


<html xmlns="" >
<head runat="server">
    <title>Adding a Server Control</title>
    <form id="form1" runat="server">
      <asp:WebPartManager ID="WebPartManager1" 
        runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
            Title="My Links">
            <asp:ListItem Value="">
            <asp:ListItem Value="">
            <asp:ListItem Value="">
            Contoso Corp.
      <asp:Button ID="Button1" runat="server" 
        Text="Delete Calendar" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Add Calendar" 
        OnClick="Button2_Click" />

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