GridView: how to add a confirmation box to delete button
Posted by Jan on June 22, 2007
In case you use delete buttons in a GridView (like below) it would be nice to ask the user for confirmation (message box).
There are two ways to achieve this:
- By using the ConfirmButtonExtender in Ajax.
- By adding a confirmation box to each row when the grid rows are bound.
The first options is the easiest to implement (only design, no code-behind). The second the most flexible: you can add information about the record in your question.
1. By using the ConfirmButtoExtender in Ajax
- Be sure Ajax is available (see ajax.asp.net).
- Add a reference to the toolkit in you web page
<%@ Register Assembly=”AjaxControlToolkit” Namespace=”AjaxControlToolkit”
TagPrefix=”ajaxToolkit” %>
- In the GridView, use a template field for the delete column containing the (image button) for the delete command.
- Add the ConfirmButtonExtender to the ItemTemplate of the delete column and link the TargetControlID to the delete button.
The source for the delete column should look something like this (the squared area is the part where the delete button is defined, the rest is the complete GridView-definition):
<asp:GridView ID=”gridOverview” runat=”server” AutoGenerateColumns=”False”
AllowSorting=”false”
DataKeyNames=”selectcolId” DataSourceID=”odcColumns” AllowPaging=”false”
OnRowCommand=”gridOverview_OnRowCommand”
OnRowDataBound=”gridOverview_OnRowDataBound”>
<Columns>
<asp:BoundField DataField=”bookTitle” HeaderText=”Title” >
ItemStyle CssClass=”grid-column-small” />
<asp:TemplateField HeaderText=”Delete” >
<ItemStyle CssClass=”grid-column-buttons” />
<ItemTemplate>
<asp:ImageButton ID=”ibDelete” runat=”server”
CausesValidation=”False” CommandName=”Delete”
ImageUrl=”~/Images/icon-delete.gif” />
<ajaxToolkit:ConfirmButtonExtender ID=”ConfirmBtExt1″
runat=”server” TargetControlID=”ibDelete”
ConfirmText=’Are you sure about deleting this record?’ />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType=”Image”
SelectImageUrl=”~/Images/icon-pencil.gif” HeaderText=”Edit”
ShowHeader=”True” ShowSelectButton=”True” CausesValidation=”False”
sItemStyle-CssClass=”grid-column-buttons” />
<asp:TemplateField HeaderText=”Order”>
<ItemTemplate>
<asp:ImageButton ID=”ibColDown” runat=”server”
CommandName=”ColDown” ImageUrl=”~/Images/down.gif”
CommandArgument=”<%# Container.DataItemIndex %>“ />
<asp:ImageButton ID=”ibColUp” runat=”server”
CommandName=”ColUp” ImageUrl=”~/Images/up.gif”
CommandArgument=”<%# Container.DataItemIndex %>“ />
</ItemTemplate>
<ItemStyle CssClass=”grid-column-buttons” />
</asp:TemplateField>
</Columns>
</asp:GridView>
For more information, see ConfirmButton
2. By adding a confirmation box to each row when the rows are bound
- Add a reference to an event handler in the GridView (see also the example above):
OnRowDataBound=”gridOverview_OnRowDataBound”
-
The delete column (ItemTemplate) should be an (image) button in a template field (replace the squared area in the example above with this code):
<asp:TemplateField HeaderText=”Delete” >
<ItemStyle CssClass=”grid-column-buttons” />
<ItemTemplate>
<asp:ImageButton ID=”ibDelete” runat=”server”
CausesValidation=”False” CommandName=”Delete”
ImageUrl=”~/Images/icon-delete.gif” />
</ItemTemplate>
</asp:TemplateField>
-
Create the event handler ‘gridOverview_OnRowDataBound’ in the code behind. When each row is bound, this procedure will add a message box to the image button.
protected void gridOverview_OnRowDataBound(object sender, GridViewRowEventArgs e)
//Add delete msgbox to each row containing field
//information to identify the row
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// reference the Delete ImageButton
ImageButton ib = (ImageButton)e.Row.FindControl(“ibDelete”);
ib.Attributes.Add(“onclick”,“javascript:return ” +
confirm(‘Are you sure about deleting ” +
DataBinder.Eval(e.Row.DataItem, “bookTitle”) + “?’);”);
}
}
Some.Net(Guy) said
I’ve been interested in trying to figure out how to do something like this. Thanks!
Sam Kasthuri said
This realy helped me .
thank you
Sam
John said
Yes, that’s great help. This is perfect post for me that i am searching.
Thanks.
John Webmaster of
http://www.seomaterial.com
Radha said
How can we handle ok button… or cancel button
bob said
Hey, do you have the css and buttons you used above?
Parwez said
Add delete msgbox to each row with ConfirmButtoExtender in Ajax
Just Add on the following in the
ConfirmText= ”
Parwez said
I think there was problem with previous post due to double cots.
Just change * to single and ** double cots.
ConfirmText= **
Bhaskar said
This is a good Example but can anyone tell me how to add Alert Message to Check Checkbox on Click of Save button and save button is on template
Means,
I have one gridview and into this 100 Checkbox and at the time of save i have to show message Please Select Atlist one Checkbox if user doesnot select any record
dwing said
I’m new to aspx and am attempting to implement option #2.
I see the confirmation dialog, but when I select ‘OK’, the only event that fires in my code behind is the Page_Load.
If I remove the confirmation dialog, then the OnRowCommand and OnRowDeleting events fire.
Any ideas?
Munene said
This sorted me out.Thanks alot
Bobby said
Is it possible to include some info (Like Title as per above example) into confirmation message.
Thank you,
Bobby
Jan said
@Bobby:
That’s not possible when you use the windows confirmation box (or you have to alter the extender). But when you use the ConfirmButton-extender, you can also create your own Modal Popup control, which gives you maximum control over the output. See http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ConfirmButton/ConfirmButton.aspx for more information.
Check out the DisplayModalPopupID-property text.
Have fun!
Mushari said
very nice
Namitha said
How do we get the index of the row we want to delete?
Namitha said
I have used ajax.but how to delete then?the row delete event is not firing in this case.
Andrei said
For #2 why not simply add OnClientClick=”return confirm(‘Are you sure?’);” to your ImageButton attributes?
Phillip Quinlan said
Great stuff! Worked out well – nice and simple to understand as well
Thanks again,
Phill