Hitting Enter in a textbox

  • Thread starter Thread starter Nate Hekman
  • Start date Start date
N

Nate Hekman

As I've mentioned a couple of times in the last few minutes(!), I've got a
simple form with an edit box and a Submit button. If I type something in
the Edit box and hit Enter I hear a click but nothing happens. If I click
on the Submit button its Click event fires as I'd expect. If I click
anywhere else on the web page and then hit Enter, the button's Click event
fires too.

I want the Click event to fire when the user is in the Edit box and hits
Enter. How can I do this?

Thanks for your help.


Nate Hekman
Calgary, Alberta, Canada
 
Is it single line or multiline? If it's multiline it's by design.
You can use clientside code to catch the ENTER press event and call the
button click.
 
It's single line. And it does reload the page--I can put a breakpoint in
Page_Load and it breaks when I hit Enter, but it doesn't call
btnSubmit_Click.
 
You can use some javascript to manage it. Add the javascript using the attributes of the control

TextBox1.Attributes.Add("onkeydown", "javascript:if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)){document.Form1." & Button1.ClientID & ".click();return false;}else return true;")

(check the name of your HTML form and your controls to get this code to work)

Hope this helps

Alan Ferrandiz [MCT]
 
Hello Curt_C [MVP]" software_AT_darkfalz.com,

Whoah... No you dont... The event needs to be hooked up and it will be executed somewhere between OnInit and OnLoad...
 
Well that starts to get pretty messy pretty fast, because of course if you
DO click the button then first Page_Load gets called, followed by
btnSubmit_Click. If you also call btnSubmit_Click from Page_Load, it gets
called twice.

Matt Berther's fix did the trick for me. Thanks to everyone who offered
advice.


Nate
 
Hello Curt_C [MVP]" software_AT_darkfalz.com,

I suppose I misunderstood... I thought you meant he needed to do something like this:

private void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Button_Click(submitButton, EventArgs.Empty);
}
}

which, of course, is completely wrong...
 
Back
Top