WebPartManager.DeleteWarning Property


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

Gets or sets a custom warning message displayed to end users when they delete a control.

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

public virtual string DeleteWarning { get; set; }

Property Value

Type: System.String

A string that contains the text of the warning message. The default value is a localized warning message.

When a user deletes a WebPart control, normally a default warning message is displayed. It warns the user that when this instance of a control is deleted, the deletion is permanent. The page developer might provide users with a way to add a new instance of the control to the page (such as through a catalog of WebPart controls, or through some programmatic means), but the current instance of a control that is deleted is permanently removed. The dialog box that displays the warning includes a button for the user to cancel the deletion, if desired.

The DeleteWarning property enables developers to set the warning message that is displayed to the user.

If a page developer assigns an empty string ("") value to this property, no warning message dialog box will be shown when a user deletes a WebPart control.


The DeleteWarning property is not displayed in the case of static WebPart controls and server controls. Static controls are server controls that are declared within a WebPartZoneBase zone in the markup of a Web page. Because such controls are static, they cannot be deleted, so the delete warning message is never displayed in that case. Static controls can be closed by a user, but a closed control is added to the page catalog, from which it can be added back to the page by a user, whereas a deleted control can never be recovered.

The following code example demonstrates the use of the DeleteWarning property declaratively and programmatically.

There are four parts to the code example:

  • 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 the custom WebPart control declared in the zone, so that a user can add it to the page at run time. Note that only dynamic controls (controls that are added to a page programmatically or from a catalog like this one) can be deleted from a page. Static controls (controls that are declared within a WebPartZoneBase zone in the markup of a page) can be closed, but never deleted. The <asp:webpartmanager> element declares a custom value for the DeleteWarning property by using the DeleteWarning attribute. The Button1_Click method assigns another custom value to the DeleteWarning property.

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

  const String NewWarning = @"If you delete this WebPart " + 
    "control instance, it will be permanently removed and " +
    "cannot be retrieved.  Do you still want to delete it?";

  protected void Button1_Click(object sender, EventArgs e)
    mgr1.DeleteWarning = NewWarning;

  // Hide the button to change the property when there is
  // no control available to delete.
  protected void Page_Load(object sender, EventArgs e)
    if (WebPartZone1.WebParts.Count == 0)
      Button1.Visible = false;
      Button1.Visible = true;

<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" 
        DeleteWarning="Do you want to delete this control?" />
      <uc1:DisplayModeMenuCS ID="menu1" runat="server" />
      <h2>Delete Warning Example Page</h2>
      <asp:WebPartZone ID="WebPartZone1" runat="server" />
      <asp:CatalogZone ID="CatalogZone1" runat="server">
              <aspSample:TextDisplayWebPart ID="text1" 
                Title="My Text WebPart" />
      <asp:Button ID="Button1" runat="server" 
        Text="Change Delete Warning" 
        OnClick="Button1_Click" />

After you load the page in a browser, you need to add the WebPart control to the page. Using the Display Mode drop-down list control, select catalog mode. When the catalog appears, select the check box next to the custom control, click Add to add it to the page, and then click Close to return the page to browse mode. Now that the control is visible, you can delete it. Using the Display Mode control again, switch the page into design mode (you cannot delete controls while the page is in browse mode). Click the verbs menu (the arrow symbol) in the header of the WebPart control, and select Delete. The warning that you set on the DeleteWarning attribute appears. Click Cancel. Now click the button labeled Change Delete Warning, which changes the property value programmatically. From the verbs menu on the control, select Delete again, and notice that this time the other warning message appears.

.NET Framework
Available since 2.0
Return to top