1. Original Entry + Comments2. Write a Comment3. Preview Comment
New comments for this entry are disabled.

June 01, 2006  |  Default button  |  8556 hit(s)

One of the many cool things in ASP.NET 2.0 is that you can declaratively set a default button. This makes the pain of the various hacks (#) pretty much go away, bye-bye.

With this in mind, I set the default button for my main blog page (yer on it right now) to be the Search button, using this syntax:
<form runat="server" DefaultButton="buttonSearch">
I quick-like tested it and it worked just fine.

But I soon got a complaint that when the page first opened, it was scrolling down a bit. I had a look, and sure enough. Now that I had a default button for the form, when the page loaded, it set the focus on the button, which was below the fold. That's not what I wanted.

The DefaultButton attribute can be applied to containers, so to fix this, I removed the attribute from the <form> tag. The box you see as Search is a <td> element in a table. So within the <td> element I added an <asp:panel> element and set its DefaultButton attribute to point to the Search button.

The morals of the story are that a) setting a default button will by default (haha) also set focus, and that therefore and/or maybe b) you want to either move your default button to an appropriate location or scope it to a suitable container. (Does this make sense? Hope so.) Anyway, I humbly submit this as your tip of the day. :-)