ASP.NET 1.x made great strides in simplifying form management and processing, but
it did miss a couple points when it came to filling forms out using the keyboard.
Fortunately these oversights were quickly noticed by users and Microsoft responded
in ASP.NET 2.0 by adding the DefaultButton and DefaultFocus properties to the HtmlForm object.
The first issue dealt with submitting forms via the "Enter" key. Users had long
ago come to expect that a form could be submitted by simply pressing the "Enter" key
once they were done entering their data. This often didn't work in ASP.NET 1.x
applications. Users were usually left looking at the form wondering what was taking the server so
long to respond. If you were lucky, the user would eventually realize the form data hadn't
been sent and would click the submit button... the less lucky ended up losing the form data
and sometimes even the user.
The new DefaultButton property addresses this issue by telling the form which
button control should post back to the server if the user doesn't select a
different one prior to pressing the "Enter" key. This way the user can
submit the form via the keyboard and still get the appropriate button handler
The other issue related to where the cursor was initially displayed on web pages that
included a form. Generally it wasn't in the form where you might expect it to be.
This meant that you couldn't just start typing if you wanted to fill out the form.
You first had to either click on or tab into one of the form fields.
It may not have been the biggest of problems, but it was an unnecessary nuisance
none the less.
The DefaultFocus property addresses this issue by simply letting you specify which control
in the form should receive the focus (and therefore the cursor) when the page loads.
This allows a user to start typing directly into the first form field as soon as the page
is done loading.
Both properties are set at the form level and couldn't be simpler to use. Simply
set the value of each to the name of the corresponding control contained in your form.
The syntax looks something like this:
Update: Using DefaultButton and DefaultFocus with MasterPages
One of our users quickly wrote in saying that he was having trouble using this tip on his site which
uses MasterPages. You see, when using MasterPages, only the master page contains the actual
form. This means that you can't simply add the DefaultButton or DefaultFocus property to the
form tag itself like we did above. If you did, the setting would be applied to every page
that used the same master page. You need a way to set the properties on a page by page basis.
Luckily it's not that hard. Instead of setting the properties on the form declaratively,
we simply set them programmatically. You'd expect it to be as simple as something like this:
Unfortunately the fact that we're using MasterPages once again makes this a little tougher.
I won't get into the technical details, but the concept is sort of like needing to
specify a full path instead of just a file name, only in this case we're talking about
controls instead of files. I really haven't used MasterPages that much, so it took me a
couple tries to get the syntax right, but my first attempt that worked looked something