From: Code Slinger on
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.

> On Thursday, August 05, 2010 10:54 AM L . Lee Saunders wrote:

> 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.
> --
> L. Lee Saunders


>> On Thursday, August 05, 2010 11:02 AM Brian Cryer wrote:

>> I have not moved to .Net 4 yet, so this is only a suggestion: Can you produce
>> a simple example with say just one control on it which exhibits this
>> behaviour - starting from a new empty project? If not then its probably
>> related to something that is been added to the page or more likley in your
>> code-behind.
>> --
>> Brian Cryer
>> http://www.cryer.co.uk/brian


>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>> Auto-Generate Code for LINQ to SQL Repository Pattern using T4
>> http://www.eggheadcafe.com/tutorials/aspnet/a7ee34d2-c297-4ec8-a933-69254242b21b/autogenerate-code-for-linq-to-sql-repository-pattern-using-t4.aspx