This documentation is archived and is not being maintained.

WebPartManager.DeleteWebPart Method

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

Permanently removes a dynamic instance of a WebPart control from a Web page.

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

public void DeleteWebPart (
	WebPart webPart
public void DeleteWebPart (
	WebPart webPart
public function DeleteWebPart (
	webPart : WebPart



The server control to be deleted.

The DeleteWebPart method permanently removes the control represented by the webPart parameter from a page. Unlike a closed control, which is added to the PageCatalogPart control and can be added back to the page, a deleted control instance can never be added back to the page.


As implemented by the Web Parts control set, the ability for users to delete a dynamic WebPart control depends on the user and the personalization scope in which the control was added to a page. If the control is added when the page is in shared scope (by a user who has permissions), then the control cannot be deleted by individual users when the page is in user scope.

Only dynamic controls can be deleted. Dynamic controls are added to a page programmatically or by users adding controls from a catalog. Static controls are added to a page declaratively in the markup or persistence format. Because the declarative tags are permanently present in the markup, static controls can never be deleted, but they can be closed and reopened.

The following code example demonstrates how to use the DeleteWebPart method. 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. Because the control is added programmatically, it is a dynamic control, and therefore it can be deleted. When a user clicks the Delete Calendar button, the code ensures that the control exists, and then deletes it by calling the DeleteWebPart 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