Export (0) Print
Expand All

WebPartManager.DeleteWarning Property

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; }
<asp:WebPartManager DeleteWarning="String" />

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.

NoteNote

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

  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;
    else
      Button1.Visible = true;
  }
</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" 
        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">
        <ZoneTemplate>
          <asp:DeclarativeCatalogPart 
            ID="DeclarativeCatalogPart1" 
            runat="server">
            <WebPartsTemplate>
              <aspSample:TextDisplayWebPart ID="text1" 
                runat="server" 
                Title="My Text WebPart" />
             </WebPartsTemplate>
          </asp:DeclarativeCatalogPart>  
        </ZoneTemplate>
      </asp:CatalogZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Change Delete Warning" 
        OnClick="Button1_Click" />
    </form>
</body>
</html>

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

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