WebPartManager.DeleteWebPart Method (WebPart)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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


Type: System.Web.UI.WebControls.WebParts.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#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<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="http://www.w3.org/1999/xhtml" >
<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="http://www.microsoft.com">
            <asp:ListItem Value="http://www.msn.com">
            <asp:ListItem Value="http://www.contoso.com">
            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" />

.NET Framework
Available since 2.0
Return to top