Disabling a button inside an UpdatePanel causes events to fire twice in Firefox

Monday, September 29, 2008 2:42:19 PM Categories: Development Technology
Rate this Content 0 Votes

Those of you who do a lot of web development like me are used to occasional issues where client side code doesn't work the same across browsers, but less common and perhaps more pesky are bugs where things run differently on the server depending on the browser. We typically don't expect things to happen differently on the server due to the browser but sometimes they do.

I ran into this strange issue where events were firing twice for postbacks from Firefox but were firing normally when using IE. When you see double postbacks happening in the log, your first thought would be that the impatient user clicked the button twice, but in this case I knew that wasn't true because I was the user and also I have code to disable the button after it is clicked to prevent double postback. There is a very common technique used by many ASP.NET developers to do this. In mojoPortal we have a helper method that encapsulated it so we just pass in the button and text to show when its disabled like this:

UIHelper.DisableButtonAfterClick(
btnUpdate,
Resource.ButtonDisabledPleaseWait,
Page.ClientScript.GetPostBackEventReference(this.btnUpdate, string.Empty) );
 

and the guts of the method look like this:

public static void DisableButtonAfterClick(
WebControl button,
string disabledText,
string postbackEventReference)
{
if (button == null) return;
button.Attributes.Add("onclick", "this.value='"
+ disabledText
+ "';this.disabled = true;"
+ postbackEventReference);
}

The irony of it is that disabling a button inside an updatepanel actually causes double post back if the browser is Firefox. So the very code we are using to try and prevent double postback actually causes it to happen. All the events fire twice, OnInit, PageLoad and the button click event. Very strange indeed. The symptom doesn't happen in IE, and removing the code that disables the button fixes the problem in Firefox and events fire normally again, that is just once. So we are back to the problem of how to prevent the user from clicking the button twice if the button is inside an UpdatePanel. Our old way works in IE (and works in general outside of UpdatePanels) but actually causes double postback in Firefox.

I did some googling and found where someone else has reported this. I also found a free dll that solves it, but its not open source so I can't use it.

I wish I was posting a solution for this problem, but for the moment I've just gone with not disabling the button. If I find a good solution I'll update this post. but thought it worth mentioning for anyone else who is seeing double postbacks in Firefox. I will say my searches found a number of things that can cause double postback in Firefox, so this isn't the only possible cause to consider. For example if a page has an <asp:Image on it and the ImageUrl has not been specified, that apparently also causes double postbacks in Firefox.

 

Copyright 2003-2010 Joe Audette

Comments

Comments are closed on this post.
Donate Money to support the mojoPortal Project. View Joe Audette's profile on LinkedIn View Joe Audette's profile on The Guild of Accessible Web Designers site