WebPartManager.DeleteWarning Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets a custom warning message displayed to end users when they delete a control.
public:
virtual property System::String ^ DeleteWarning { System::String ^ get(); void set(System::String ^ value); };
public virtual string DeleteWarning { get; set; }
member this.DeleteWarning : string with get, set
Public Overridable Property DeleteWarning As String
Property Value
A string that contains the text of the warning message. The default value is a localized warning message.
Examples
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 (!string.IsNullOrEmpty(input.Text))
{
_contentText = input.Text + @"<br />";
input.Text = String.Empty;
DisplayContent.Text = this.ContentText;
}
}
}
}
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class TextDisplayWebPart
Inherits WebPart
Private _contentText As String = Nothing
Private _fontStyle As String = Nothing
Private input As TextBox
Private DisplayContent As Label
Private lineBreak As Literal
<Personalizable(), WebBrowsable()> _
Public Property ContentText() As String
Get
Return _contentText
End Get
Set(ByVal value As String)
_contentText = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
Controls.Clear()
DisplayContent = New Label()
DisplayContent.BackColor = Color.LightBlue
DisplayContent.Text = Me.ContentText
Me.Controls.Add(DisplayContent)
lineBreak = New Literal()
lineBreak.Text = "<br />"
Controls.Add(lineBreak)
input = New TextBox()
Me.Controls.Add(input)
Dim update As New Button()
update.Text = "Set Label Content"
AddHandler update.Click, AddressOf Me.submit_Click
Me.Controls.Add(update)
End Sub
Private Sub submit_Click(ByVal sender As Object, _
ByVal e As EventArgs)
' Update the label string.
If input.Text <> String.Empty Then
_contentText = input.Text + "<br />"
input.Text = String.Empty
DisplayContent.Text = Me.ContentText
End If
End Sub
End Class
End Namespace
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>
<%@ Page Language="vb" %>
<%@ Register TagPrefix="uc1"
TagName="DisplayModeMenuVB"
Src="~/DisplayModeMenuVB.ascx" %>
<%@ Register TagPrefix="aspSample"
Namespace="Samples.AspNet.VB.Controls"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Private Const NewWarning As String = "If you delete this WebPart " & _
"control instance, it will be permanently removed and " & _
"cannot be retrieved. Do you still want to delete it?"
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
mgr1.DeleteWarning = NewWarning
End Sub
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs)
If WebPartZone1.WebParts.Count = 0 Then
Button1.Visible = False
Else
Button1.Visible = True
End If
End Sub
</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:DisplayModeMenuVB 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.
Remarks
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.
Note
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.