Default Button Click On Enter KeyPress On Web Form

  • Thread starter Thread starter Joey
  • Start date Start date
J

Joey

I have an asp.net 1.1/C# web form with a text box server control and
one button server control. Users can type in text to search and then
click the button to start the search. I want them to be able to type
in text and then just hit the Enter key instead of having to click the
button.

Currently, an Enter keypress causes a post back, but it does not fire
the event handler for the button click. How can I get this to work?
 
Not sure if this is "right", but what I do is do the update in the load
event; but only if the cancel button hasn't been clicked;

if (Page.IsPostback)
{
if (Request.Form["cmdCancel"] != null)
return;

// Save the data
}

So pressing [enter] will trigger a postback that will save, as will pressing
your submit button. Pressing the cancel button causes the postback but the
update code is not run.

Would be interested if someone has a "proper" way.
 
Back
Top