Confirm Message Box before Deleting
By Pritam Baldota
Confirm Message Box before Deleting in Datagrid in ASP.Net is very useful in all applications.
Confirm Message Box before Deleting
Scenarion I
To use confirm Message box before delete operation uses JavaScript for confirmation box. You have to add JavaScript on button.
Page_Load
if(!IsPostBack) btnConfirm.Attributes.Add("onclick","return confirm('Do you want to Delete');");
Once clicked Ok button on Confirm Box it executes Button_Click server event else doesn’t do anything.
btnConfirm_Click
//Your server code to delete
Scenario II
The DataList and DataGrid controls easily allow to add a "Delete" button/hyperlink in your template. When clicked, this button/link raises a DeleteCommand event that you can handle to delete the data item.
<asp:datagrid id="dg" runat="server"AutoGenerateColumns="False">
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
DataGrid’s Template Delete Button raises DeleteCommand event after clicking Delete Button of DataGrid. We needs to write javascript confirm method to this template button. This should be write in DataGrid’s ItemCreated event because it fires when datagrid’s item has been created.
private void dg_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//Check for Item and Alternate Items if(e.Item.ItemType==ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.Item)
{
Button btn=(Button)e.Item.Cells[0].Controls[0];
btn.Attributes.Add("onclick","return confirm(‘Do you want to continue?’);");
}
}
Popularity (39873 Views)
Article Discussion: Confirm Message Box before Deleting
Confirm Message Box
hello
use this
'Shows a confirmation box when button delete is clicked
BtnDelete.Attributes.Add("onclick", "return confirm('are you sure you want to delete? ');")
c#.net
BtnDelete.Attributes.Add("onclick", "return confirm('are you sure you want to delete? ');");
good one here
hello

The javascript code for it is simple:
function confirm_delete()
{
if (confirm("Are you sure you want to delete the custom search?")==true)
return true;
else
return false;
}
Using code-behind, you can attach the javascript popup dialog to the button:
_myButton.Attributes.Add("onclick", "return confirm_delete();");
If the button is inside a repeater (in this case called Searches), for example, which most of mine our, you have to attach it as follows by handling the ItemCreated event:
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.Page_Load);
Searches.ItemCreated += new RepeaterItemEventHandler(this.Item_Created);
Searches.ItemCommand += new RepeaterCommandEventHandler(this.DeleteSearch_Click);
}
private void Item_Created(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
ImageButton _myButton = (ImageButton)e.Item.FindControl("btnDelete");
_myButton.Attributes.Add("onclick", "return confirm_delete();");
}
}
private void DeleteSearch_Click(object sender, RepeaterCommandEventArgs e)
{
int _id = Int32.Parse(e.CommandArgument.ToString());
// Do Your Thing...
}
Inside your repeater, you add the button as such:
... ImageButton id=btnDelete ImageUrl="images/icon_delete.gif" runat="server" CommandArgument='<%# ((DataRowView)Container.DataItem)["ID"] %> ...
When you click on the delete button, the javascript popup dialog asks if you want to delete the search. If you choose “cancel“, your “DeleteSearch_Click” event is never fired. If you choose “ok”, the event is fired and you can delete the item.
Adds a bit of professionalism to your web applications without requiring a lot of effort.

if the link button is inside a datalist
Sneha Mishra replied
to K Pravin Kumar Reddy at Friday, September 29, 2006 4:46 AM
if the link button is inside a datalist what would be the code for deleting an item using .net version 1.1
Cancel also deletes!
Ibrahim Bello replied
to K Pravin Kumar Reddy at Friday, September 29, 2006 4:46 AM
Hi, I tried the above javascript code, confirm_delete() but to my chagrin cancel also deletes, even close deletes.
I'm coding in VB.NET. My page inherits from a masterpage. Thanks
ButtonColumn Confirmation in VB.net
Jass singh replied
to Pritam Baldota at Friday, September 29, 2006 4:46 AM
Dear sir,
Following is ur code for C#.
Can u pls send same for vb.net...
required urgently
Thanks & Regards
jass
---------------------------------------------------------------------------------
DataGrid’s Template Delete Button raises DeleteCommand event after clicking Delete Button of DataGrid. We needs to write javascript confirm method to this template button. This should be write in DataGrid’s ItemCreated event because it fires when datagrid’s item has been created.
private void dg_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//Check for Item and Alternate Items if(e.Item.ItemType==ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.Item)
{
Button btn=(Button)e.Item.Cells[0].Controls[0];
btn.Attributes.Add("onclick","return confirm(‘Do you want to continue?’);");
}
}
Confirmation message on delete button on some condition.
Pradeep Thorat replied
to Jass singh at Friday, September 29, 2006 4:46 AM
The confirmation message should pop up on delete button click but after checking some condition. If the condition is false the confirmation message should not pop up. If there is some code for this please let me know. It's very urgent.
Thanks in advance.
eliza replied
to Pritam Baldota at Friday, September 29, 2006 4:46 AM
Lets have an Employee table in which some employee records are saved with the unique identity numbers for each employee. Now, suppose some records were deleted from the Employee table due to some reasons and now I want all the deleted records from Employee table,which may not be possible now as records are deleted from the table,but we can get the list of Unique Identity records deleted from Employee table by using query.
To demonstrate this first of all we have to create an Employee Table with some records inserted to it.Then we can delete some selected records from the Employee table to verify the output.
jimmy replied
to eliza at Friday, September 29, 2006 4:46 AM
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// <script language="javascript">
// function CheckConfirm() {
// if (confirm('Are you sure you want to delete?'))
// __doPostBack('Buttonx_Click', '');
// else
// return false;
// return true;
// }
///script>
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//switch(Convert.ToInt32(DropDownList1.SelectedValue.ToString()))
//{
// case 1: Buttonx.Attributes.Add("onclick", "alert('opcion 1!')"); break;
// case 2: Buttonx.Attributes.Add("onclick", "alert('opcion 2!')"); break;
//}
switch(Convert.ToInt32(DropDownList1.SelectedValue.ToString()))
{
case 1: Buttonx.Attributes.Add("onclick", " if(confirm('¿Seguro que ha leido las condiciones del contrato 1?')) this.form.submit();"); break;
// case 2: Buttonx.Attributes.Add("onclick", " CheckConfirm();"); break;
case 2: Buttonx.Attributes.Add("onclick",
" if(confirm('¿Seguro que ha leido las condiciones del contrato 2?'))
__doPostBack('Buttonx_Click', ''); else return false;"); break;
case 3: Buttonx.Attributes.Add("onclick", "
if(confirm('¿Seguro que ha leido las condiciones del contrato 3?'))
__doPostBack('Buttonx_Click', ''); else return false;"); break;
case 4: Buttonx.Attributes.Add("onclick", "
if(confirm('¿Seguro que ha leido las condiciones del contrato 4?'))
this.form.submit(); else return false;"); break;
}
}
}
protected void Buttonx_Click(object sender, EventArgs e)
{
Label1.Visible = true;
Label1.Text = "validacion";
Response.Redirect("Default2.aspx");
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// TextBox1.Text = DropDownList1.SelectedValue.ToString();
}
}
