AllowClose Property

WebPart.AllowClose Property


Gets or sets a value indicating whether an end user can close a WebPart control on a Web page.

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

public virtual bool AllowClose { get; set; }

Property Value

Type: System.Boolean

true if the control can be closed on a Web page; otherwise, false. The default value is true.

After a user closes a WebPart control on a Web page, the control is no longer visible or available on the page. The closed control is added to the page catalog, a Web Parts entity that stores a reference to the control. If a developer adds a PageCatalogPart control to the page within a CatalogZone control, users are able to switch the page to catalog display mode, select the closed control in the page catalog, and add it back to the page.


A closed WebPart control can be added back to a page either programmatically, or by a user who selects the closed control from the page catalog when the page is in catalog display mode.

Closing a WebPart control is different from deleting it. A closed control can be added back to a page, while a deleted control is permanently removed. For more information on deleting controls, see the DeleteWebPart method. Closing a control is also different from hiding it. A control that is hidden is still present on the page, still participates in page life cycle events, and is only hidden from the user's view, but a closed control is not even rendered on a page.

Both static and dynamic WebPart controls (static controls are declared in a page's markup, while dynamic controls are added programmatically) can be closed.

If a developer sets the AllowClose property to false, a close verb does not appear on the control, and the user is not able to close the control.

This property cannot be set by themes or style sheet themes. For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

The personalization scope of this property is set to Shared and can be modified only by authorized users. For more information, see PersonalizableAttribute and Web Parts Personalization Overview.

The following code example demonstrates how to change the default setting of the AllowClose property for a custom WebPart control, so that it cannot be closed.

The first part of this example contains the code for a custom WebPart control named TextDisplayWebPart. Note that in the constructor of the custom control, the TextDisplayWebPart.AllowClose property is set to false, which has the effect of preventing users from closing the control on a Web page. This means that the close verb on the verbs menu of the control will be disabled for users. 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 code example assumes that you compile the source code into an assembly, place it in a Bin subfolder of your Web application, and reference the assembly with a Register directive in your Web page. For a walkthrough that demonstrates both methods of compiling, see Walkthrough: Developing and Using a Custom Web Server Control.

using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls
  public class TextDisplayWebPart : WebPart
    private String _contentText = null;
    TextBox input;
    Label DisplayContent;

    public TextDisplayWebPart()
      this.AllowClose = false;

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

    protected override void CreateChildControls()
      DisplayContent = new Label();
      DisplayContent.BackColor = 
      DisplayContent.Text = this.ContentText;
      input = new TextBox();
      Button update = new Button();
      update.Text = "Set Label Content";
      update.Click += new EventHandler(this.submit_Click);
      ChildControlsCreated = true;

    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 second part of the example shows how to reference the TextDisplayWebPart control in an ASP.NET Web page. Note that in the <aspSample:TextDisplayWebPart> element that references the control, you could also change the value of the property set by the control's constructor. To allow the control to be closed, simply add an AllowClose="true" attribute to the element in the declarative markup.

<%@ page language="C#" %>
<%@ register tagprefix="aspSample" 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
  <form id="Form1" runat="server">
    <asp:webpartmanager id="WebPartManager1" runat="server" />
      title="Zone 1"
        <parttitlestyle font-bold="true" ForeColor="#3300cc" />
          bordercolor="#81AAF2" />
            title = "Text Content WebPart" />

.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft