views:

445

answers:

4

What is the best solution of defaultButton and "Enter key pressed" for ASP.NET 2.0-3.5 forms?

+1  A: 

Just add the "defaultbutton" attribute to the form and set it to the ID of the button you want to be the default.

<form defaultbutton="button1" runat="server">
    <asp:textbox id="textbox1" runat="server"/>
    <asp:button id="button1" text="Button1" runat="server"/>
</form> 

NOTE: This only works in ASP.NET 2.0+

Wayne
A: 

I don't want any button to be default, and it hits first one which is submit..

dimarzionist
A: 

Maybe you need to clarify your question then, because it sounds like that's exactly what you're asking for...

Craig Eddy
+1  A: 

Since form submission on hitting the enter key is a part of life with HTML, you'll have to trap the Enter key using javascript and only allow it to go through when it's valid (such as within textareas). Check out http://brennan.offwhite.net/blog/2004/08/04/the-single-form-problem-with-aspnet/ for a good explanation.

Wayne