views:

350

answers:

3

This seems to be a common problem but I cannot find a solution.

I type in my username and password which are in a login control I have created. I then press enter once I've typed in my password and the page just refreshes. It triggers the page load event but not the button on click event.

If I press the submit button then everything works fine.

A: 

Pressing ENTER on a text input executes Form.onsubmit, not Button.onclick.

I suppose this was inspired by the fact that you can have a form without an actual submit button (depending solely on the use of ENTER).

Jonathan Lonowski
+1  A: 

If you're using ASP.NET 2.0 or higher, you can set a default button attribute for your page's Form:

<form runat="server" DefaultButton="SubmitButton">
Jon Limjap
will this work if the button is inside a control?
WebDude
+3  A: 

using your forms default button is correct, but you need to supply it the correct id as it will be rendered to HTML.

so you do as Jon said above:

<form runat="server" DefaultButton="SubmitButton">

But ensure you use the Button name that will be rendered. You can achieve this my making the Button public in your control, or a method that will return it's ClientId.

Let's say your button is called btnSubmit, and your implementation of your control ucLogin.

Give your form an id

<form runat="server" id="form1">

Then in your page load in your code behind of your page, set the DefaultButton by handing it your button client id.

protected void Page_Load(object sender, EventArgs e)
{
   form1.DefaultButton = ucLogin.btnSubmit.ClientID;
}
WebDude
Is clientID a bug? Looks like it's frm.DefaultButton = btn.UniqueID;
Praesagus