views:

48

answers:

3

Hi,

I have a form on ASP .NET page, with button onclick event attached to doPostBack function. However when I click that button form gets submitted twice. Server side debugger enters Page_Load twice, Fiddler records two aspx requests etc, there are no 302 redirects etc, it's pretty straightforward page.

When I set breakpoint in __doPostBack function it's also called once (perhaps it's just an illusion).

Is there a chance to debug client side even more deeper to find the issue?

Thanks, Paweł

+1  A: 

Do you wire up the event handlers manually? If so, you should check if AutoEventWireup="true" in the ASPX's Page directive. If so, change it to false or remove the manual wiring. It can happen when you upgrade an old page to the newer web application format with a designer file.

cjberg
dragonfly
either have autowireup=true *or* remove manual wiring.
TheGeekYouNeed
With AutoEventWireup="True", ASP.NET will automatically wire up common event handlers such as Page_Load, so you don't need to do it by hand (it exists to help you avoid such uninteresting boilerplate code). But if you're doing it in the code, you must set AutoEventWireup="false", or else you end up with the event handlers registered twice and thus called twice as well. I'd recommend removing the manual wiring, because less boilerplate code equals better readability, which is important.
cjberg
Here's a question about AutoEventWireup, with answers that explains how it work:http://stackoverflow.com/questions/680878/what-does-autoeventwireup-page-property-mean
cjberg
The reason why I asked about it, is that it won't solve my issue, which is: TWO REQUESTS ARE SENT FROM THE BROWSER :) and calling Page_Load twice is just a implication.Thanks
dragonfly
A: 

It sounds like there are two things causing a postback. Is the button a submit button and also has an onclick handler? That would cause this behavior.

Are the 2 ASPX requests you see in Fiddler the same, or just slightly different?

davisoa
Those requests contain the same form data etc...It's input, type="image" with onclick="__doPostBack...".
dragonfly
A: 

Ok. got it: input type="image" had

onclick="__doPostBack(.....)" 

When I modified it to:

onclick="_doPostBack(...); return false;"

and two requests issue went away. Only one request was recorded by Fiddler, Page_Load is called once and everything is neat.

So be careful when using HTML elements that cause automatic form submission on click ;)

Thanks, Paweł

dragonfly
I'm glad you found it. The same thing has happened to me as well. Today I always avoid using the onclick attribute directly, favoring jquery's bind/click instead. That way you get a consistent programming model that doesn't vary between browsers, and a better separation of markup and JavaScript code.
cjberg