Confirm Message Box before Deleting By Pritam Baldota Printer Friendly Version
|  |
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?’);"); }
}
|
| |
| Article Discussion: Confirm Message Box before Deleting |
| Pritam Baldota posted at 29-Sep-06 04:46 |
| Original Article |
 |
|
| |
| Confirm Message Box |
| K Pravin Kumar Reddy replied to Pritam Baldota at 29-Sep-06 04:55 |
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 |
| K Pravin Kumar Reddy replied to Pritam Baldota at 29-Sep-06 04:57 |
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 31-Aug-08 04:39 |
| if the link button is inside a datalist what would be the code for deleting an item using .net version 1.1 |
 |
|