locked
Button click event handler firing twice RRS feed

  • Question

  • User11445494 posted
    I noticed today that button click event handlers are firing twice for me in some cases when the button is inside an UpdatePanel -- but only in Firefox.  I'm wondering if this might have to do with the Firefox 2.0.0.1 update that came out today because I never noticed this problem until today.  Has anyone else run into this problem?
    Wednesday, December 20, 2006 5:03 PM

All replies

  • User-751836609 posted

    you need to post your sample code which fires an event twice

    RG

    Thursday, December 21, 2006 9:42 AM
  • User903972397 posted

    One possible reason:  Check your button declaration in your .aspx source.  If you have a 'runat=server' and onclick="button1_click", and you have an event handler in your code-behind (ie. .aspx.vb), it will cause the event to be fired twice.  Here is an example in xxx.aspx: 

    <asp:Button id="button1" onclick="button1_Click" runat="server" Text="Click Me!"></asp:Button>

    This declaration should be:   

    <asp:Button id="button1" runat="server" Text="Click Me!"></asp:Button>
    Good luck!
    Saturday, December 23, 2006 11:24 PM
  • User727661388 posted

     Hai ,

      You are right I face the same problem, and thanks for your post above, I solve this issue by the above post
     

    Thursday, August 9, 2007 3:08 AM
  • User-2032131020 posted

    hi,

    you are a saviour, i have been wondering why my event was firing two times, and i was suspecting AJAX Accordian panel. But when i removed the on lcik from .aspx page it works like a charm.

    thanks again

     MM

    Thursday, May 29, 2008 10:43 AM
  • User-760020091 posted

    I disagree with the posted solution.
    The reason the event is firing twice is because you have an "OnClick" attribute AND an event handler in code behind.

    Markup:
     OnClick = "btnLogin_Click"

    Code Behind:
    this.btnLogin.Click += new System.Web.UI.ImageClickEventHandler(this.btnLogin_Click);

    You will find the event handler in a region called "Web Form Designer generated code".  This was the OLD way .NET handled events for buttons.  ASP .NET 3.5 now creates OnClick for all new buttons.

    To keep your code consistent, I would argue that the better way to fix the problem is to delete the Event Handler in Code Behind, not the OnClick attribute.

    Anyone agree with this?

    Friday, August 1, 2008 11:23 AM
  • User555306248 posted

    Pages support an automatic way to bind their events to methods.

    If the AutoEventWireup attribute of the @ Page directive is set to true (or if it is missing, because by default it is true), page events are automatically
    bound to methods that use the naming convention of Page_event, such as Page_Load and Page_Init.

    The AutoEventWireup attribute requires that the page event handlers have specific, predictable names.

    If you want to create your own names for page event handlers, you can set AutoEventWireup to false and then bind the events to the methods explicitly.

    If you include explicit binding, you must make sure that the AutoEventWireup is disabled so that the method is not called twice,
    once automatically by AutoEventWireup and another time by your explicit binding.

    For example, in Visual Basic, you can use the Handles keyword to perform the binding.

    For C#

    this.PreRender += new EventHandler(MyFunction_PreRender);

    you will have to wire up your Page load manually -

    this.Load += new EventHandler(Page_Load); 

    delete the Event Handler in Code Behind and generate new one

    also set in page tag AutoEventWireUp="false"

    May this solve your problem

    Sunday, August 3, 2008 11:16 PM
  • User-1423034775 posted

    Thank u ,its helped me a lot . 

    Saturday, December 19, 2009 5:36 AM
  • User-1302055314 posted

    i have a accordian and contain a link button , when im click the button at the first time it wont work but click it again itll work what is the wrong with that

    Monday, May 17, 2010 8:29 AM
  • User-1763871356 posted

    I noticed today that button click event handlers are firing twice for me in some cases when the button is inside an UpdatePanel -- but only in Firefox.  I'm wondering if this might have to do with the Firefox 2.0.0.1 update that came out today because I never noticed this problem until today.  Has anyone else run into this problem?

    I have the same problem when debugging. I have a button inside an update panel that is firing the click event twice in firefox but not IE. My event handler calls Response.Redirect at the end to redirect to the next page, but in FireFox that doesn't work because of the second click. I have AutoEventWireup = true and onclick="btnContinue_Click". I have no other event wireup coded in the codebehind. Without either of these two the event is not fired at all.  I don't understand why onclick has to be set if AuotEventWireup is true, but why the event is firing twice in this case also seems to be unanswered here.

    Monday, July 12, 2010 6:37 PM
  • User1423204276 posted
    this might help you out
     
    private bool Ok2Delete(int bypassCount)
    {
            Session["delCount"] = Session["delCount"] == null ? 0 : Session["delCount"];
            return !((int)(Session["delCount"] = (((int)Session["delCount"]) + 1) % bypassCount) == 0);
    
    }
    just call it at the beginning of your RowDeleting event handling method. Example:
    if (!Ok2Delete(2)) return; // parameter of 2 if event is firing twice
                                    // 3 if trice, etc...
    reff: http://forums.asp.net/p/1002747/1324414.aspx
     
     
    happy coding...
    Tuesday, October 19, 2010 2:42 AM
  • User-1763871356 posted

    It turned out, in my case, that someone had added some code to the Page_Load that attempted to disable the button after it was clicked by adding javascript to it. That javascript was explicitly triggering a postback because in IE, the button click was not. In firefox, the button click was triggering a postback so it was doing two postbacks.

    Tuesday, October 19, 2010 8:30 AM
  • User-1764133772 posted

    I had the same problem in asp.net/c#.  Nothing worked per the blogs.  I ended up deleting my whole project, downloading new code from TFS, and bingo, it worked.  Apparently the Rebuild feature doesn't always clean itself up very well.

    Wednesday, August 17, 2011 6:46 PM
  • User-1627823045 posted

    Hi,

    Thank for example, BUT I am building asp.net website. And here i have to specify the "onclick="button1_Click" else the event wont fire.

    Please help me out.

    -AJ

    Thursday, August 25, 2011 1:13 AM
  • User324972980 posted

     

    iupchris10 is correct!  I'm upgrading a asp.net 1.1 to asp.net 4.5.  This fixed my issue.

    Thanks!

    Monday, April 21, 2014 1:07 PM
  • User-2087820430 posted

    I had the same problem and thank you for the solution, my project works very well !!!!

    Wednesday, July 29, 2015 12:05 PM