Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

WebPartManager.DeleteWebPart Method

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
)

Parameters

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.

NoteNote

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.