I am using a GridView in ASP.Net 2.0 to Insert, Edit, Update and Delete. I am using ItemTemplate and EditItemTemplat e and the buttons are LinkButtons. I am inserting a new row from the footer. The RowCommand event is firing twice when I hit the Edit, Update, Delete and Insert buttons. The AutoEventWireup is set to false. What am I missing? Thanks.
GridView RowCommand firing twice
Collapse
X
-
I have a solution to this issue that is probably the cleanest I have seen. I will allow you to make the fewest changes to your code and continue using the RowDeleting and RowDeleted events for the GridView.
Currently when you build a command field for a delete button it will look something like this.
<asp:CommandFie ld ButtonType="Ima ge" DeleteImageUrl= "images/delete.gif" ShowDeleteButto n="true" />By Changing the ButtonType to "Link" and modifying the DeleteText you will have the same delete image that works exactly like the Image Button Type but without the double firing event. Here is the modified code.
<asp:CommandFie ld ButtonType="Lin k" DeleteText="<im g src='images/delete.gif' alt='Delete this' border='0' />" ShowDeleteButto n="true" />Additionally, I am constantly being asked about how to add a confirm dialog box to the delete button. You can use the following code on the RowDataBound event to add the confirmation.
If e.Row.RowType = DataControlRowT ype.DataRow ThenI hope this helps!
Dim lnk As LinkButton = e.Row.Cells(1). Controls(0)
lnk.OnClientCli ck = "if(!confirm('A re you sure you want to delete this?')) return false;"
End If -
use this trick
Change the Button Field as Link Button:
Button field as Link button and, in the text, set de tag of a html image
<img src="IMAGENAME" >
and the event fires one time
<asp:ButtonFiel d Text="<img src=Images/edit.gif />" HeaderText="Edi tar" >
</asp:ButtonField >Comment
Comment