Hitting Enter in a textbox

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
 
C

Curt_C [MVP]

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.
 
N

Nate Hekman

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.
 
A

Alan Ferrandiz [MCT]

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]
 
M

Matt Berther

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...
 
N

Nate Hekman

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
 
M

Matt Berther

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top