Mladen Prajdić Blog

Blog about stuff and things and stuff. Mostly about SQL server and .Net

"Fun" with Submit buttons and pressing Enter in ASP.Net 2.0

I had some fun this weekend with ASP.Net 2.0 Master pages and submit buttons when pressing Enter key. The catch here is that with the introduction of Master pages there's only one form per page which is specified in the master page.

UI simplicity for users has been pressing the Enter key when they wanted to submit the form data.

In older versions of ASP(.Net) you could put any number of forms on the page. The only rule was that they couldn't be nested. When you were in a form and you pressed Enter the browser used the submit button of that form and submited the form. (I won't be going into how browsers did or didn't implement this)

Because ASP.Net with Master pages can have only one Form with runat="server" tag and multiple forms are impossible this was solved in a very nice way.

Both Form and asp:Panel have a property called DefaultButton. The value of this property has to be set to the ID of the button you want to be clicked when the user presses Enter.

In my case I have a search on the master page and so the DefaultButton is set in the Form tag to the value of the "Search" button ID.

I have a form that has to be submitted to save data in the child page so i wrap up the content in asp:Panel control and set it's DefaultButton to my "Save" button's ID.

It works like a charm.

These are examples of use. I've omitted some attributes like OnClick handlers etc.

<!-- Master page -->
<form id="frmMaster" runat="server" defaultbutton="btnSearch">
    <!-- Other input controls -->
    <asp:Button ID="btnSearch" runat="server" Text="Search" />
<span class="kwrd">&lt;</span><span class="html">asp:ContentPlaceHolder</span> <span class="attr">ID</span><span class="kwrd">=&#34;ContentBody&#34;</span> <span class="attr">runat</span><span class="kwrd">=&#34;server&#34;</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">asp:ContentPlaceHolder</span><span class="kwrd">&gt;</span>

</form>

<!-- Child page -->
<asp:Content ID="content" ContentPlaceHolderID="ContentBody" runat="Server">
    <asp:Panel ID="Panel1" runat="server" Width="100%" DefaultButton="btnSave">
        <table>
            <!-- Other input controls -->
            <tr>
                <td>
                <asp:Button ID="btnSave" runat="server" Text="Save" />
                </td>
            </tr>
        </table>
   </asp:Panel>
</asp:Content>

Legacy Comments


bojanv
2006-09-11
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
be carefull by setting default button. Be aware, that causeValidation must be properly set in order to perform user validation or leave it alone. Just a thought... ;)

Mladen
2006-09-11
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
thanx for pointing that out Bojan.

dave
2006-10-04
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
your welcome! oh and the d in your defaultbutton="btnSearch">
is not in red!!!!!!

James D
2007-06-27
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
Thanks for a quick a simple answer!

Tony McKinney
2007-07-06
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
Thank you! I was working on some crazy javascript solution I saw and considering multiple forms controls (which won't work too well with my masterpages). You rock.

Stefan Wrobel
2007-07-19
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
Thank you so much! I had been looking all over for the solution to this exact problem, and this solved it for me! Thanks for helping me navigate this tangled web of ASP.NET!

rewrew
2007-09-11
eehrew
dfsdfff

UC
2007-11-12
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
GREAT solution!
4 tumbs up XD

Matt
2007-11-28
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
Awesome! Google really needs to move this up the search engine results pages -- I tried all that other crap before this, and this was the simplest and most efficient solution.

Pavz
2008-02-06
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
A simple solution explained in an elegant way. Thanks!!!

Matthew Key
2008-07-30
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
Does this defo work with Firefox

it doesnt for me, work fine in IE though

Mladen
2008-07-30
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
apparently no it doesn't. but don't take my word for it. try it :)

Maheshkumar
2008-08-26
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
You ROCK man

Shannon
2008-10-16
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
Awesome solution! Simple & elegant!! I'm using VS2005 and this is exactly what I was looking for!!

Dougi
2009-04-21
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
Thanks for this solution. Do you know a way of doing this dynamically though? I have a page which uses panels to change the layout and want to change the default submit button on ENTER with the change of panel visibility.

Mladen
2009-04-21
re: "Fun" with Submit buttons and pressing Enter in ASP.Net 2.0
@dougi
no idea. sorry