SharePoint Experts, Information Architects, Expert Witness

SICG provides a broad array of business and technology consulting from architecture to design to deployment of global systems with a focus on surfacing data in the enterprise. We focus on the "How", not just the possible. Contact me direct: david_sterling@sterling-consulting.com or call 704-873-8846 x704.

Search This Blog

Tuesday, March 18, 2008

Button Click Event does not fire on the first click

Having come across this several times and forgotten how I'd fixed it, I figured this will help more than a few...

The problem occurs in Buttons, DataGrids and other event driven controls in Web Parts and Controls. On the first time through, the primary event (like the Click event on a button), the event does not fire. On the second click however, everything seems fine. If you follow through in Debug, you'll file that the event is actually skipped and the control proceeds to the render event.

In most cases, the fix is simple: one of the controls within the control/web part has AutoPostBack = true where it doesn't need it. Typically, this is a cut and paste error - for example, adding AutoPostBack to a TextBox.

This, by the way, though very odd, is somewhat expected. The invalid postback on the control tells ASP.NET that it's already happened, when indeed it hasn't since the specific control cannot perform the actual postback.

2 comments:

GeoffE said...

I had this problem it was caused by using a 'Server.Transfer' instead of a 'Response.Redirect'.

I looked at the logs and could not figure out why the page was doing what appeared to be a Page Load when I should have been hitting the Button Click event.

Hope this helps someone.

GeoffE said...

I found a button click failed to work the first time when I transferred control to another page and then came back to click the first time. Instead of a button click event I was seeing a PageLoad event(non postback 'an initial page load').

I was using a Server.Transfer 'url' to be efficient. Changing the transfer to a Response.Redirect fixed my problem of the button click event not firing the first time.

Hope this helps someone.