Export (0) Print
Expand All
Expand Minimize

WebPartManager.CloseWebPart Method

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
)

Parameters

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

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

ExceptionCondition
ArgumentNullException

webPart is null.

ArgumentException

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
{
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    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()
    {
      Controls.Clear();
      DisplayContent = new Label();
      DisplayContent.BackColor = Color.LightBlue;
      DisplayContent.Text = this.ContentText;
      this.Controls.Add(DisplayContent);

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

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

    }

    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" 
    TagName="DisplayModeMenuCS"
    Src="~/DisplayModeMenuCS.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.CS.Controls"%>

<!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 Button1_Click(object sender, EventArgs e)
  {
    mgr1.CloseWebPart(text1);
  }

</script>

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

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

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

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.

Show:
© 2014 Microsoft