Tuesday, August 17, 2010

Confirm Message Box before Deleting in Datagrid in ASP.Net is very useful in all applications.


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.



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

No comments: