ASP.Net 4.0 - Events firing twice.

  • Thread starter Thread starter L . Lee Saunders
  • Start date Start date
L

L . Lee Saunders

HELP!

Any time a control that posts using DoPostback (ie, via javascript) the
event is fired twice on the server.

Example: Regular asp:Buttons do not fire twice but autopostback
dropdownlists do as well as <Button runat="server" OnServerClick="asdf">
controls.

INFO: asp.net 4.0 C#
dev machines: both XP & 7
master pages with lots of ASCX controls on each page for control reuse.

(Though, the double fire even happened on the login page where there are no
ascx controls)

Please help, this is bringing down the project.
 
Are you using jQuery and hooking client-side onChange via jquery.change?

We've run into this same problem in an ASP.NET 3.5 AND 4.0 project to which we added jQuery 1.4.2, and it is causing us significant grief as well.

To demo the problem, I created an empty website project and added a single aspx page. To that page I added a SCRIPT reference to jQuery and a single autopostback control (I've verified that it happens with both textbox and dropdown). No other controls exist on that page (e.g. no IMGs with empty SRC attribs, which is a long-standing bug that also causes double postback).

<asp:TextBox ID="autopostbackcontrol" runat="server" AutoPostBack="true" />

<script type="text/javascript">
$(document).ready( function() {
$('<%= autopostbackcontrol.ClientID %>').change( function () { alert('Double postback coming up!'); } )
});
</script>

Doesn't matter what you put inside the jQuery.change handler; removing the alert or having it return a bool or even making a completely empty handler doesn't impact the double postback behavior.

It also doesn't matter if you wireup the server-side TextChanged eventhandler declaratively or programmatically (obviously you shouldn't do both or you would be causing the double postback yourself).

Stepping into javascript shows that the ASP.NET-generated onchange="__doPostback..." is executing twice. But the jQuery.change is only executing once because the alert only displays once. And occasionally, onchange only fires a single time, posting back once, so the bug doesn't seem to manifest consistently.

If you comment out the jQuery.change, it reverts to the normal desired single postback behavior.

I have found no other solution than to disable AutoPostBack and to execute __doPostback at the end of the jQuery eventhandlers for all controls that must run both client- and server-side code.
 
Back
Top