If you use image as the button type in gridview, the rowcommand event will be executed twice when it is triggered. It is said that it only appears in IE6, IE5, firefox, etc. There is no problem. My IE7 also has the same problem. It seems to be related to the IIS version. I look forward to Microsoft patch.
I encountered a problem this afternoon. The rowcommand event of the gridview was executed twice, and the postback was also executed twice. At first, I suspected that it was a problem with the debugger. After checking the log, I confirmed that it was actually executed twice.
I checked the code n times and eliminated it sentence by sentence, but still no problem was found. I started to suspect that it was the mouse. I captured the packet and found that two requests were indeed sent, and the first request was not processed. Occasionally, I changed the button type from image to button, and it unexpectedly worked. It was only executed once. It seems that the problem only lies in Imagebutton.
Through google search, I found that many people have encountered this problem, and it is definitely an asp.net bug. Clicking the button will send two requests to iis. The first time is eight bytes less than the second time, causing the first request to be unsuccessful, and then it will be sent again. Therefore, the easiest thing to think of is to ignore the first request, as follows This is the first solution.
There are currently two solutions, neither of which are perfect. The first is to add the following statement to the rowcommand event:
if (Request["x"] == null && Request["y"] == null)
Response.End();
Through this statement, the coordinates of the image are determined. If the coordinates are null, it is the first request. Because the first request is invalid, end it.
The second one is recommended by Microsoft: try not to use imagebutton in gridview, but use link or button. If you must use it, you can use similar code:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat=server id="ImageButton1" CommandName="Delete"
ImageUrl="" commandargument='<%# DataBinder.Eval(Container,
"RowIndex") %>' OnCommand="ImageButton1_Command" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void ImageButton1_Command(object sender, CommandEventArgs e) {
GridView1.DeleteRow(Int32.Parse(e.CommandArgument.ToString()));
}
Add imagebuttong to TemplateField and call deleterow in the event.
I don’t know if there is a better solution. The following forum discusses this problem in detail:
http://www.developersdex.com/asp/message.asp?p=1116&r=4641456&page=2 The result of the discussion on the first page is: This problem will only occur when the type of button is image, which can be skipped Don’t read it, there are detailed discussions and solutions on the next two pages.
I have been encountering strange problems all day today, and my head is spinning. The same code can often be executed for a while, but not for a while, and strange things keep happening in vs2005. I just simulated several problems that could not be run in the afternoon at home and it worked fine! ! I'm going to have a headache again tomorrow
http://www.cnblogs.com/bluewater/archive/2006/11/21/567871.html