I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 218, comments - 2281, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

"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" />
    
    <asp:ContentPlaceHolder ID="ContentBody" runat="server">
    </asp:ContentPlaceHolder>
</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>

Print | posted on Sunday, September 10, 2006 7:52 PM | Filed Under [ .Net ]

Feedback

Gravatar

# 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... ;)
9/11/2006 12:24 PM | bojanv
Gravatar

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

thanx for pointing that out Bojan.
9/11/2006 12:27 PM | Mladen
Gravatar

# 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!!!!!!
10/4/2006 12:29 PM | dave
Gravatar

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

Thanks for a quick a simple answer!
6/27/2007 2:20 PM | James D
Gravatar

# 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.
7/6/2007 5:51 AM | Tony McKinney
Gravatar

# 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!
7/19/2007 9:24 PM | Stefan Wrobel
Gravatar

# eehrew

dfsdfff
9/11/2007 12:34 PM | rewrew
Gravatar

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

GREAT solution!
4 tumbs up XD
11/12/2007 2:32 PM | UC
Gravatar

# 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.
11/28/2007 8:20 PM | Matt
Gravatar

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

A simple solution explained in an elegant way. Thanks!!!
2/6/2008 10:41 PM | Pavz
Gravatar

# 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
7/30/2008 1:39 PM | Matthew Key
Gravatar

# 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 :)
7/30/2008 3:32 PM | Mladen
Gravatar

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

You ROCK man
8/26/2008 7:13 PM | Maheshkumar
Gravatar

# 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!!
10/16/2008 9:46 PM | Shannon
Gravatar

# 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.
4/21/2009 8:27 AM | Dougi
Gravatar

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

@dougi
no idea. sorry
4/21/2009 9:56 AM | Mladen
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET