Confirm Message Box before Deleting

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.


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.

//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">
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>

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?’);");


By Pritam Baldota   Popularity  (45213 Views)