Connecting Tech Pros Worldwide Forums | Help | Site Map

GridView RowCommand firing twice

Newbie
 
Join Date: May 2007
Posts: 4
#1: May 16 '07
I am using a GridView in ASP.Net 2.0 to Insert, Edit, Update and Delete. I am using ItemTemplate and EditItemTemplate 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.

Newbie
 
Join Date: Jun 2007
Posts: 1
#2: Jun 19 '07

re: GridView RowCommand firing twice


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:CommandField ButtonType="Image" DeleteImageUrl="images/delete.gif" ShowDeleteButton="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:CommandField ButtonType="Link" DeleteText="<img src='images/delete.gif' alt='Delete this' border='0' />" ShowDeleteButton="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 = DataControlRowType.DataRow Then
Dim lnk As LinkButton = e.Row.Cells(1).Controls(0)
lnk.OnClientClick = "if(!confirm('Are you sure you want to delete this?')) return false;"
End If
I hope this helps!
Newbie
 
Join Date: Aug 2006
Posts: 1
#3: Jan 9 '09

re: GridView RowCommand firing twice


http://bytes.com/topic/net/answers/6...d-firing-twice

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:ButtonField Text="&lt;img src=Images/edit.gif /&gt;" HeaderText="Editar" >

</asp:ButtonField>
Reply