Default button on a web form

At yesterday’s MSDN Roadshow event in Manchester (if you were there, apologies again for the temperature in the presentation room. I was melting and I had the luxury of stepping outside after my sessions - I was impressed so many of you stuck it out for so long) someone asked me a specific question about the use of multiple Atlas:UpdatePanel controls on a page (you can do this – and you have a great deal of control over the update behavior of each UpdatePanel) and specifying the default button that is actioned when a user hits the [Enter] key on a page. How do you control this?

Well, in ASP.Net 2.0 there is a new property on the form and asp:Panel controls called “DefaultButton” which allows you to set the button which is “selected” when the Enter key is pressed. (For more details take a look at: https://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx). If you’re using Master Pages then you’ll need to set the default button programmatically (eg in the page load event handler) by using something like:

this.Form.DefaultButton = MyButton.UniqueID;

You need to do this programmatically as you need the UniqueID of the control (once ASP.NET has “mangled” it with the content control name etc). DefaultButton sometimes hangs around with the DefaultFocus property whose job it is to define which control gets the initial focus when the page loads…