The discovery and solution of a bug in asp.net

Source: Internet
Author: User
If an image is used for the button type in the gridview, The rowcommand event will be triggered twice. It is said that it only appears in IE6, IE5, firefox, and so on. The same problem exists in IE7, it seems to have something to do with the IIS version. We look forward to Microsoft's patches.
This afternoon, when a problem occurs, the rowcommand event of the gridview will be executed twice, and the postback will be executed twice. At first, I suspect that the fault is caused by the debugger. After checking the log, make sure the task is actually executed twice.
I checked the code n times and ruled out it one by one. I still couldn't find the problem. I began to suspect that it was a mouse. I caught the packet and found that the first request was not processed. Occasionally, the buttontype is changed from image to button. Unexpectedly, it is only executed once. It seems that the problem is only caused by Imagebutton.
Many people found this problem through google search. It is definitely a bug in asp.net. Click the button to send two requests to iis. The first request is eight bytes less than the second request, causing the first request to fail and then resend the request, therefore, the easiest way to handle this problem is to ignore the first request.
There are currently two solutions that are not perfect. The first one is to add the following statement to the rowcommand event: if (Request ["x"] = null & Request ["y"] = null)
Response. End ();

This statement is used to determine the coordinate of an image. If the coordinate is null, it is the first request because the first request is invalid and ends.
The second one is recommended by Microsoft: Try not to use imagebutton in the gridview, but use link or button. If necessary, 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 are any better solutions. The following Forum will discuss this issue in detail:
Http://www.developersdex.com/asp/message.asp? P = 1116 & r = 4641456 & page = 2 the result of the first page is: this problem occurs only when the button type is image, the following two pages will detail the discussion and solution.
Today, I have encountered a strange problem all over the world. The same code can be executed for a while, but it cannot be executed for a while. vs2005 has made a strange mistake. Just now, I simulated several problems that failed to run in the afternoon at home and it turned out to work well !! Another headache tomorrow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.