My Software Notes

Useful things I discover

Archive for September 2010

Postback after disabling a button (ASP.NET)

with 2 comments

I’ve seen it on tons of sites.  You click the button, it becomes disabled and the page posts.  The disabling is so you don’t go clicking it multiple times while waiting for the server processing to complete.  

It’s all over the place, so it must be simple right?

Well, actually, once you find out how to do it, yes it is simple, but getting there?  What a pain.

I foolishly thought I could just add another even handler for the button click using jQuery. Nope.  Although jQuery adds the event handler rather than replacing existing event handlers, the action of disabling the button seems to kill the ASP.NET postback script.

Well, of course, I should be using the “OnClientClick” property of the asp:Button control.  No problem.  Nope.  That doesn’t work either.  It overwrites the ASP.NET postback script. 

This is ridiculous.  So it’s time to start asking Google.  The first few results tell me to add the ASP.NET postback script in server code and other real complex solutions.  These just seemed way too complex to me.  I have a rule of thumb that I follow at times like this: “If it’s getting more complex you’re going in the wrong direction.”

So I hunted some more and sure enough here is the simple answer: Disable a button control during postback.

Totally simple but what a pain to discover.

Written by gsdwriter

September 9, 2010 at 9:39 am

Posted in Uncategorized