I Have a gridview with a command button for delete defined.
On the Datasource for the grid I have a DeleteCommand that runs a stored procedure that takes theID field defined in grid view control.
When the delete command is selected in the gridview the deletecommand stored procedure is called twice with the same ID (I have traced this).
I have removed all delete eventsfrom the gridview and datasource.
I have a FormView that has a selectcommand using the GridViews primary ID.
I have autoeventwireup set to true.
There is no code being ran before or after deletion.
I did have a refresh procedure being ran after delete. When this was active Instead of the same record being deleted twice, the selected record and the record below in the grid were both being deleted with one call.
Any Ideas?
|
| fourhundred Tuesday, May 02, 2006 5:13 PM |
for anybody interested there's a workaround for this problem: insert the following lines in your RowCommand event:
if (Request["x"] == null || Request["y"] == null) { Response.End(); }
this will check for the mouse position on the image; and the "fake" postback does not have these values set. not a proper solution, but at least a "hack".
if anybody knows the reason for this behaviour or at least a decent way to solve the problem i'd be thankfull for him/her to post it here. (actually i'd expect ms to come up with a solution for a problem with their control, but it seems that i'm expecting too much.)
|
| Sandro Rudin Thursday, May 18, 2006 8:09 AM |
Here is my work around, that seems to work nicely. If you use a TemplateField and an ImageButton, you do NOT have to handle the button event.
The GridView sees the CommandName=Delete and handles it:
<asp:TemplateField > <ItemTemplate> <asp:ImageButton
id="DeleteButton" CommandName="Delete"
ImageUrl="~/images/delete.ico" runat="server" /> </ItemTemplate> </asp:TemplateField>
That's all, no double postback, no custom event handling of the delete. It would be nice if the CommandField worked correctly though! |
| fuul Thursday, May 18, 2006 7:22 PM |
Is the Delete button located on a BindingNavigator? If yes, have you set the DeleteItem property of the BindingNavigator to None? |
| vkh75 Wednesday, May 03, 2006 9:54 AM |
The delete button is a "delete" configuredCommandField in the GridView. In the ASP.NET application I have found the following to be true.
If I set the ButtonType to image and specify a deleteimage url for the "delete" command field then the Datasources DeleteCommand fires twice.
If I set the ButtonType to button for the "delete" CommandField then the datasources deletecommand is properly fired once.
I would like to display a delete image in the GridView.
|
| fourhundred Wednesday, May 03, 2006 1:55 PM |
Sorry, I thought it was about Windows Forms...
http://forums.asp.net/ |
| vkh75 Wednesday, May 03, 2006 5:57 PM |
I have the same problem! When the delete CommandField type is "Image", Delete fires twice!
Has anyone figured out why this happens?
|
| fuul Tuesday, May 16, 2006 11:00 PM |
this seems to be a known (and serious) issue that makes the gridview practically useless. i have seen several posts all dealing with that same problem, some of them even dating from last year. unfortunately ms doesn't seem to care... at least i didn't find any solutions posted yet.
btw: it seems that the problem doesn't correspond with the delete button, but occurrs when setting buttontype=image (no matter what command). |
| Sandro Rudin Wednesday, May 17, 2006 10:08 AM |
for anybody interested there's a workaround for this problem: insert the following lines in your RowCommand event:
if (Request["x"] == null || Request["y"] == null) { Response.End(); }
this will check for the mouse position on the image; and the "fake" postback does not have these values set. not a proper solution, but at least a "hack".
if anybody knows the reason for this behaviour or at least a decent way to solve the problem i'd be thankfull for him/her to post it here. (actually i'd expect ms to come up with a solution for a problem with their control, but it seems that i'm expecting too much.)
|
| Sandro Rudin Thursday, May 18, 2006 8:09 AM |
for anybody interested there's a workaround for this problem: insert the following lines in your RowCommand event:
if (Request["x"] == null || Request["y"] == null) { Response.End(); }
this will check for the mouse position on the image; and the "fake" postback does not have these values set. not a proper solution, but at least a "hack".
if anybody knows the reason for this behaviour or at least a decent way to solve the problem i'd be thankfull for him/her to post it here. (actually i'd expect ms to come up with a solution for a problem with their control, but it seems that i'm expecting too much.)
|
| Sandro Rudin Thursday, May 18, 2006 8:09 AM |
Here is my work around, that seems to work nicely. If you use a TemplateField and an ImageButton, you do NOT have to handle the button event.
The GridView sees the CommandName=Delete and handles it:
<asp:TemplateField > <ItemTemplate> <asp:ImageButton
id="DeleteButton" CommandName="Delete"
ImageUrl="~/images/delete.ico" runat="server" /> </ItemTemplate> </asp:TemplateField>
That's all, no double postback, no custom event handling of the delete. It would be nice if the CommandField worked correctly though! |
| fuul Thursday, May 18, 2006 7:22 PM |
i have already seen thatsolution somewhere, but unfortunately that does not work for me. the reason is that when using a template field you need to rebind the grid after the postback in order to get the events of the controls in the template fieldfired -and this is exactly what i'm trying to prevent because my grid is showing the results of a rather complex search and having to bind it twice (and therefore execute the search twice) significantly lowers the performance. using the gridview commands helps me a lot there because the rowcommands get fired without the need to rebind. |
| Sandro Rudin Wednesday, May 24, 2006 10:45 AM |
Same problem here! Tried too design my crm app a bit as a programming break to find out that design influences functionality, should be taken care of. After three days of guessing where I went wrong I changed as a last option my nice delete image to buttontype="button" and no way, you're kidding, it works totally fine now! But looks like...*.*
Yours sincerely! |
| deBoo Tuesday, August 22, 2006 1:38 PM |
This seems to have fixed my problem with it firing 2 times, however in my function, it's only firing "Select" command when I select on this button. I'm thinking it could be because I have it setup to select entire row when click anywhere...but is there a middle ground where I can make it run delete command when that field is selected?
<asp:TemplateField > <ItemTemplate> <asp:ImageButton id="b_Delete" CommandName="Delete" ImageUrl="~/images/Ex.GIF" runat="server" /></ItemTemplate></asp:TemplateField>
protected void gv_Files_RowCommand(object sender, GridViewCommandEventArgs e)
{
lbl_ErrorMes.Text = e.CommandName;
if (e.CommandName == "Delete")
Delete_Record();
}
protected void gv_Files_RowDataBound(object sender, GridViewRowEventArgs e)
{
//This tells the mouse click to select entire row
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes[ "onclick"] = ClientScript.GetPostBackEventReference(this.gv_Files, "Select$" + e.Row.RowIndex);
e.Row.Style[ "cursor"] = "hand";
}
}
|
| GaiaGeek Wednesday, October 11, 2006 12:07 PM |
Hi,
Did you solve your problem? I just spent 3 days fixing similar problems with the gridview, this is the first time I use the edit feature of the GridView.
Like everyone, during these few days, I complain a lot about the product quality of microsft, how come they produce such a difficult control with BUGS reported every where, I thought.
As usual, at last, ( 3 days almost 24 hours of work and learning) I found that most of my (I don't know whether it is yours), is due to a databinding call made to the grid inside the Page_Load event handler - every time there is a post back, the grid is databind again, before calling the RowCommand, RowUpdate...
void Page_Load()
{
if (Page.IsPostback)
{
DataBindingCall(); // this causes a lot of trouble. This method with bind the gridview to a data source.
// After I remove this call, a lot of troubles are gone.
....
}
else
{
}
}
I hope my experience is useful to you or other gridview users.
|
| skypekid Friday, October 20, 2006 5:12 PM |
I found the problem to be with the Grid HTML declaration itself.
I was under the impression that tohave my delete event handler (dgResults_DeleteCommand) execute when the DataGrid's DeleteCommand event firesI had to specify OnDeleteCommand="dgResults_DeleteCommand" in the DataGrid's declaration. This turns out to be wrong or at least removing it from the grid declaration stopped the delete event firing twice�which was causing me grief & anguish as my coding buddy left work on-time as I was still sitting there stumped!
Grid that fires the delete event procedure TWICE....
<asp:DataGrid runat="server" ID="dgResults" BorderWidth=1px AutoGenerateColumns="False" Width="98%" HorizontalAlign=Center BackColor="AliceBlue" AlternatingItemStyle-BackColor="Lavender" AllowSorting=True HeaderStyle-BackColor="#FFFFFF" HeaderStyle-Font-Size="Small" HeaderStyle-ForeColor="#003366" ItemStyle-Font-Size="9pt" OnDeleteCommand="dgResults_DeleteCommand" OnItemDataBound="dgResults_ItemDataBound"> <Columns> columns ..... </asp:DataGrid>
Grid that fires the delete event procedure ONLY ONCE....
<asp:DataGrid runat="server" ID="dgResults" BorderWidth=1px AutoGenerateColumns="False" Width="98%" HorizontalAlign=Center BackColor="AliceBlue" AlternatingItemStyle-BackColor="Lavender" AllowSorting=True HeaderStyle-BackColor="#FFFFFF" HeaderStyle-Font-Size="Small" HeaderStyle-ForeColor="#003366" ItemStyle-Font-Size="9pt"> <Columns> columns ..... </asp:DataGrid>
|
| maccab Monday, October 30, 2006 6:38 AM |
Also, the problem is also present in the DataGrid with all the events. This bug was introduced in asp.net 2.0 because in 1.1 this situation did not happen.
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="<img src=Images/edit.gif />" HeaderText="Editar" >
</asp:ButtonField>
I just hope microsoft could fix this problem promptly.
- Proposed As Answer byRohbanian Wednesday, August 05, 2009 6:27 AM
-
|
| RCUELLO Friday, December 15, 2006 9:29 AM |
I think that the event is bubbling up, firing for the image/button, and then the grid column.
You probably have to catch the image event and cancel the event from bubbling up to the grid. |
| fuul Tuesday, December 26, 2006 1:35 AM |
This works fine. Thanks - Siva |
| Siva Shan Wednesday, January 17, 2007 4:32 AM |
How about this:
if (!Page.ClientScript.IsOnSubmitStatementRegistered(this.GetType(), "fixTwice")){
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "fixTwiceVar", "var isSubmited = false;", true);
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "fixTwice", "if(isSubmited){return false;}else{isSubmited = true};");
} |
| Partenon Monday, February 12, 2007 2:55 PM |
|
| tigerAct Wednesday, April 25, 2007 11:42 AM |
Hallo RCUELLO,
you are the greatest! It works fine! |
| Frank Uhlig Sunday, December 23, 2007 11:24 PM |
Thanks a lot , it works.
|
| Murat Demiray Monday, December 31, 2007 6:18 AM |
Well folks, i had the same problem with an ImageButton, and i also tried almost every trick. I used the imageButton to Upload Files in my application. I even was thinking in declare a static variable in the method or search in my File List (a listBox) just to avoid this issue. The easiest way to solve my problem was to delete the "Handles" part of the code after the event declaration. I thought this wouldn't work but it didt it just fine. I hope this helps you guys.
|
| William RamĂrez Wednesday, September 03, 2008 8:29 PM |
Awesome Dude . Thank you for your wisely solution. Mehregan |
| Rohbanian Wednesday, August 05, 2009 6:29 AM |