 |
|
|
 |
|
 |
Thanks Leo Muller, This code really very useful for me...
|
|
|
|
 |
|
 |
I copied the code. It works fine with delete button. But The message shows on Edit/Cancel/Update button as well. But I want the message only on delete. any help?
|
|
|
|
 |
|
 |
Hi Leo,
I copied the code and i kept in my project. it is prompting message box, but the rows are getting deleted in either i press yes or no. Please help me. its urgent.
Naresh Kumar .P Happy coding.
|
|
|
|
 |
|
 |
Well, maybe the problem is the explorer ( for imagebutton IE trigger 2 times the event onclick). try with linkbutton instead of ImageButton.
I mean:
protected void gvRequisitos_RowCreated(object sender, GridViewRowEventArgs e) { foreach (DataControlField DCF in gvRequisitos.Columns) { if (DCF.ToString() == "CommandField") { CommandField Cf = (CommandField)DCF;
if (Cf.ShowDeleteButton) { int indComando = gvRequisitos.Columns.IndexOf(DCF);
foreach (Control ctrl in e.Row.Cells[indComando].Controls) { if (((LinkButton)ctrl).CommandName == "Delete") { ((LinkButton)ctrl).OnClientClick = "javascript:return confirm('¿Eliminar registro?') ";
Image im = new Image(); im.ImageUrl="~/Imagenes/delete.gif"; ((LinkButton)ctrl).Attributes["Text"] = ""; ((LinkButton)ctrl).Controls.Add(im); } }
} } } }
hope help you.
modified on Monday, August 11, 2008 12:03 AM
|
|
|
|
 |
|
 |
Hi,
Is there a way to apply this code to a delete linkbutton created by the AutoGenerateDeleteButton process?
thanks, Dave
|
|
|
|
 |
|
 |
Here what I am using. It's a derivate from the above code:
/// <summary> /// If the gridview has AutoGenerateDeleteButton set to true, then /// it add a confirm message to the delete buttons. /// This function should be called in the RowDataBound event /// </summary> protected static void AddConfirmDelete(GridView gv, GridViewRowEventArgs e) { // Make sure we have the delete button showing. if (gv.AutoGenerateDeleteButton != true) return;
// Make sure we are processing a DataRow if (e.Row.RowType != DataControlRowType.DataRow) return;
// Search for the Delete button foreach (DataControlFieldCell dcf in e.Row.Cells) { // The header for the LinkButton is normally empty. if (dcf.Text == "") { // Search the list of control and file the Delete link foreach (Control ctrl in dcf.Controls) { LinkButton deleteButton = ctrl as LinkButton; if (deleteButton != null && deleteButton.Text == "Delete") { deleteButton.Attributes.Add("onClick", "return confirm('Are you sure you want to delete this row?');"); break; } } break; } } }
|
|
|
|
 |
|
 |
Yves Tr's solution worked perfectly for me. Thanks!
|
|
|
|
 |
|
 |
good work... i was looking for same
|
|
|
|
 |
|
 |
"I could be mistaken, but this article describes a process that adds a delete confirmation to the command button cell, not the delete command button itself. Working with a CommandField using ImageButtons, canceling the confirmation does not actually cancel the delete command. So while the confirmation does come up, it does not actually allow the user to change their mind as the button still calls the post back. Please let me know if I am just missing something." - sephoratrading
|
|
|
|
 |
|
 |
Use a template field for the button, then do button.ClientClick = "return confirm('Are you sure?')". Simple as pie.
|
|
|
|
 |
|
 |
Protected Sub grdCountry_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCountry.RowCreated For Each dcf As DataControlField In CType(sender, GridView).Columns If dcf.ToString = "CommandField" Then With CType(dcf, CommandField) If .ShowDeleteButton Then If TypeOf (e.Row.Cells(CType(sender, GridView).Columns.IndexOf(dcf))) Is DataControlFieldCell Then For Each ctrl As Control In e.Row.Cells(CType(sender, GridView).Columns.IndexOf(dcf)).Controls If TypeOf (ctrl) Is Button AndAlso CType(ctrl, Button).CommandName = "Delete" Then CType(ctrl, Button).OnClientClick = "ret=confirm('Bu kaydı silmek istediğinizden emin misiniz?'); if (!ret) return false;" End If Next End If End If End With End If Next End Sub
|
|
|
|
 |
|
 |
This looks fairly much the same to me, so it should work! Also nice to have the code in VB.NET

Leo
|
|
|
|
 |
|
 |
The difference is that MetAK's method puts the confirmation prompt on the delete button and not on the entire column. If you have a single CommandField for Edit & Delete, your method will cause the prompt to display if Edit or Delete is clicked.
|
|
|
|
 |
|
 |
if (e.Row.RowType != DataControlRowType.DataRow) return; int lastCellIndex = e.Row.Cells.Count - 1; ImageButton deleteButton = (ImageButton)e.Row.Cells[lastCellIndex].Controls[0]; deleteButton.OnClientClick = "if (!window.confirm('Opravdu chcete smazat tento záznam?')) return false;";
|
|
|
|
 |
|
 |
i had done this, in gridview, i add <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" /> then, in .cs file, i add your code. But, the confirm of the delete event, not occur.
Could u explain it, tx.
|
|
|
|
 |
|
 |
You are right, the gvTenders should be the gridview object
|
|
|
|
 |
|
 |
what's the object????
tx
-- modified at 3:32 Tuesday 15th May, 2007
|
|
|
|
 |
|
|
 |
|
 |
That is a good question!
I recommend that you try to re-use your code as much as possible. This will make it much easier to maintain.
In this case, this is a function that will be used from many places in your project, so best is to put this in its own class. When you call the function, the static method will allow you to call it, without creating an instance of your class first.
And yes, it does only save one line, but then on the other hand, that is 50% of the code needed here 
Leo
|
|
|
|
 |