• Sign In
 
  • MSDN Blogs
  • Microsoft Blog Images
  • More ...
Common Tasks
  • Blog Home
  • Email Blog Author
  • RSS for comments
  • RSS for posts
Search
  • Advanced search options...
Tags
  • .NET Framewor
  • .NET Framework
  • Ajax/Javascript
  • ASP.NET
  • CLR
  • Cool stuff
  • DataAccess
  • Debugging/Windbg
  • Hotfix/Service Pack
  • IDEVDataCollector
  • IIS
  • Internet Explorer
  • Italian techs
  • LogParser
  • OT
  • Personal
  • Productivity
  • Random
  • Scripting/ASP
  • Security
  • Technology
  • Tools
  • Troubleshooting
  • Vista/Longhorn
  • Visual Studio
Archives
Archives
  • November 2010 (1)
  • October 2010 (1)
  • July 2010 (2)
  • April 2010 (1)
  • March 2010 (2)
  • February 2010 (2)
  • January 2010 (1)
  • October 2009 (2)
  • September 2009 (2)
  • August 2009 (1)
  • July 2009 (5)
  • June 2009 (1)
  • May 2009 (1)
  • April 2009 (3)
  • March 2009 (3)
  • February 2009 (5)
  • January 2009 (3)
  • December 2008 (5)
  • November 2008 (3)
  • October 2008 (2)
  • September 2008 (3)
  • August 2008 (3)
  • July 2008 (3)
  • June 2008 (5)
  • May 2008 (4)
  • April 2008 (8)
  • March 2008 (4)
  • February 2008 (5)
  • January 2008 (2)
  • December 2007 (4)
  • November 2007 (6)
  • October 2007 (6)
  • September 2007 (8)
  • August 2007 (6)
  • July 2007 (7)
  • June 2007 (10)
  • May 2007 (9)
  • April 2007 (12)
  • March 2007 (8)
  • February 2007 (5)
  • January 2007 (3)
  • December 2006 (1)
  • November 2006 (4)
  • October 2006 (2)
  • September 2006 (9)
  • August 2006 (2)
  • July 2006 (1)

If an EventHandler is called twice...

MSDN Blogs > Never doubt thy debugger > If an EventHandler is called twice...

If an EventHandler is called twice...

Carlo Cardella
28 Mar 2007 4:38 PM
  • Comments 4

...take a close look at your code smile_regular.

This comes from a case I worked on in the last couple of days: a customer reported a problem in one of his ASP.NET 2.0 pages, which is meant to perform some sort "long running" operation on the server. What puzzled me a bit (well... quite a bit, at the beginning) is the fact that after the user clicked posted the page (clicking on a submit button) and the long operation begun on the server, after about 90 seconds they noticed a new click button for the submit event was fired, thus restarting the whole "long" operation (and of course leading of all sort of problems this implies for the application's logic).

Doing some tests we found that more that the above, even if he closed the browser on the client immediately after submitting the form, after about 90 seconds they still got a second click event firing for the submit button! smile_whatchutalkingabout Of course if the browser is closed on the client, it's hard to believe that the new click and subsequent post is coming from the user... smile_sarcastic

We looked and the page source produced by the server, here is an interesting excerpt:

<input type="button" name="ctl00$ContentPlaceHolder$ButtonSubmit" value="Submit data" 
    onclick="this.disabled=true; this.value='Please wait...';
    __doPostBack('ctl00$ContentPlaceHolder$ButtonSubmit','');
    __doPostBack('ctl00$ContentPlaceHolder$ButtonSubmit','')" 
    id="ctl00_ContentPlaceHolder_ButtonSubmit" 
/>

(note that I broke the onclick Javascript line value only for formatting purposes in this post, that was on a single line) 

It is clear that ASP.NET generate that button with wrong content for the onclick event handler, note the duplicated "__doPostBack(...) call". Question is: why?

The customer sent me the click event handler method and the button declaration, which looked like the following:

<asp:Button ID="ButtonSubmit" runat="server" CausesValidation="False" 
    Text="Submit data" OnClick="ButtonSubmit_Click" 
    UseSubmitBehavior="false" 
/>

(they also had AutoEventWireup="true" in their @Page directive, and this is what actually put us on the right track even if not directly involved in this problem)

The customer then reviewed more closely some of their code (unfortunately he inherited this project and did not worked directly on the code, so he didn't know all the details...), and found that in the Page_Load handler they dynamically added a Javascript call to __DoPostBack(...) using code similar to the following:

ButtonSubmit.OnClientClick = ButtonSubmit.Page.ClientScript.GetPostBackEventReference(ButtonSubmit, null);

So, now is clear that the problem was cause by both ASP.NET adding the callback handler because of UseSubmitBehavior="false" in the button declaration, and the GetPostBackEventReference(...) call in the Page_Load method; the customer was therefore able to fix the problem simply changing the button declaration to use UseSubmitBehavior="true" (which, by the way, is the default, read the MSDN reference for further details.

 

Cheers

  • 4 Comments
ASP.NET
Leave a Comment
  • Please add 3 and 7 and type the answer here:
  • Post
Comments
  • Lewis
    12 May 2007 11:24 PM

    I ran into the same problem, when the client clicks on the button on a grid view they will confim if they wish to delete the file. If they click ok it fires the click event for the button. Then it fires the event again for some reason i added everything using the property field and the template editor. Here is the code.

    <asp:Button ID="Button1" runat="server" CommandArgument='<%# Eval("UserName") %>'

                           CommandName="DeleteUser" Text="Delete user" OnClientClick="return confirm('Are you sure you want to delete this User?')" />

  • Carlo Cardella
    13 May 2007 4:35 AM

    Hi Lewis, with a simple test on my machine I can't repro the problem so I can't tell you exactly what's going on in your app, sorry; but I can suggest you to have a look at the HTML code in IE ("view source") and try to identify if there is any "duplicated" javascript code which may submit the page twice.

    HTH

  • David Day
    13 Nov 2008 9:38 AM

    I have a similar issue.  A vanilla aspx page: a message with a single button which is sent as an email to the client.  When the button is clicked in Outlook, we get two server calls.  The first, using the debugger, has all the data in it.  The second is empty.  Sadly it's the second one which gets rendered.  The correct page processing is lost.  

    The calls are in different sessions - so no ViewState or Session fiddling can be employed.  Trying to figure out what is triggering the second call and where it is coming from.  

    There is no javascript in the email page.  Submit behaviour is used, and autowireup, but there are no events wired in.

  • Carlo Cardella
    14 Nov 2008 2:39 AM

    David, do you actually mean OWA or the full Outlook client? Which is the correlation between the aspx page and Outlook?

Page 1 of 1 (4 items)
  • © 2012 Microsoft Corporation.
  • Terms of Use
  • Trademarks
  • Privacy Statement
  • Report Abuse
  • 5.6.402.223