WebPartManager.CloseWebPart Method (WebPart)


Closes a WebPart control in such a way that it is not rendered on a Web page, but can be reopened.

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

public void CloseWebPart(
	WebPart webPart


Type: System.Web.UI.WebControls.WebParts.WebPart

A WebPart or server control that is being closed in a WebPartZoneBase.

Exception Condition

webPart is null.


webPart is not in the Controls collection.

- or -

webPart is a shared control and has already been closed by another user.

The CloseWebPart method removes a WebPart or other server control so that it is not rendered on the Web page that originally contained it. The closed control is added to a PageCatalogPart object, which maintains a reference to the closed control and makes it possible for the control to be restored to the page. A closed WebPart control still appears in the collection referenced by the WebParts property.

Closing a control is different from deleting it. Closed controls are still available to be restored to a page, but a deleted control instance is permanently removed and can never be restored. Regardless of whether a WebPart or server control is static (declared in the markup of a page) or dynamic (added to the page programmatically or by a user from a Web Parts catalog), it can be closed and reopened on a page.

Typically, users can close a WebPart control by clicking its verbs menu and selecting the close verb. A control can also be closed by directly calling the CloseWebPart method and passing it a reference to webPart.

On a page where WebPart controls have been closed, if a developer declares an <asp:catalogzone> element, and within it adds an <asp:pagecatalogpart> element, it provides a simple user interface (UI) for users to restore closed controls to the page at run time. Users can switch the page into catalog display mode, and the closed controls will appear within the page catalog. Users can select closed controls and add them back to the page at whatever position they desire, and then the selected controls are restored to the page and rendered as normal.

When the CloseWebPart method is called, it raises several events: WebPartClosing, SelectedWebPartChanging (if there are multiple controls), and WebPartsDisconnecting (if there are connected controls). Normally developers can cancel these events, but in certain cases it is not possible to cancel them. For details, see the documentation for the WebPartClosing, SelectedWebPartChanging, and WebPartsDisconnecting events.

The following code example demonstrates how to use the CloseWebPart method.

The code example has four parts:

  • A user control that enables you to change page display modes.

  • A custom WebPart control.

  • A Web page.

  • An explanation of how the example works in a browser.

The first part of the code example is the user control for changing display modes. You can obtain the source code for the user control from the Example section of the WebPartManager class overview. For more information about display modes and how the user control works, see Walkthrough: Changing Display Modes on a Web Parts Page.

The second part of the code example is the custom WebPart control. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. This example uses the dynamic compilation approach; thus there is no Assembly attribute in the Register directive for this control at the top of the Web page. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom Web Server Control.

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls
    Level = AspNetHostingPermissionLevel.Minimal)]
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class TextDisplayWebPart : WebPart
    private String _contentText = null;
    TextBox input;
    Label DisplayContent;
    Literal lineBreak;

    [Personalizable(), WebBrowsable]
    public String ContentText
      get { return _contentText; }
      set { _contentText = value; }

    protected override void CreateChildControls()
      DisplayContent = new Label();
      DisplayContent.BackColor = Color.LightBlue;
      DisplayContent.Text = this.ContentText;

      lineBreak = new Literal();
      lineBreak.Text = @"<br />";

      input = new TextBox();
      Button update = new Button();
      update.Text = "Set Label Content";
      update.Click += new EventHandler(this.submit_Click);


    private void submit_Click(object sender, EventArgs e)
      // Update the label string.
      if (input.Text != String.Empty)
        _contentText = input.Text + @"<br />";
        input.Text = String.Empty;
        DisplayContent.Text = this.ContentText;


The third part of the code example is the Web page. The page contains a CatalogZone zone, with an <asp:pagecatalogpart> element declared within it. This is what will contain the closed WebPart control and enable users to add it back to the page. The Button1_Click method directly calls the CloseWebPart method to close the custom WebPart control, although a user can also close the control through the verbs menu.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc1" 
    Src="~/DisplayModeMenuCS.ascx" %>
<%@ Register TagPrefix="aspSample" 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
      <asp:WebPartManager ID="mgr1" runat="server" />
      <uc1:DisplayModeMenuCS ID="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
          <aspSample:TextDisplayWebPart ID="text1" 
            Title="My Text WebPart" />
      <asp:CatalogZone ID="CatalogZone1" runat="server">
          <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />   
      <asp:Button ID="Button1" runat="server" 
        Text="Close WebPart" 
        OnClick="Button1_Click" />

After you load the page in a browser, close the custom WebPart control by clicking the verbs menu (the arrow symbol) in the header of the control, and clicking Close. Now change the page to catalog mode by selecting Catalog in the Display Mode drop-down list control. The page catalog appears with the closed control. Select the check box next to the closed control, click Add to add it to the page, and then click Close to return the page to browse mode. The control is restored to the page. Now close it again, this time by clicking the Close WebPart button.

.NET Framework
Available since 2.0
Return to top