Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 50 Time: 03:26 - Dec 21, 2006  

david

Submit when pressing enter?

How do I make it so that if a user is pressing the enter key when typing in a textbox, the form will be automatically submitted?

Categories

Answer Discussion
Tutorials

 

david

Date:: Dec 21, 2006

Time:: 03:54

Actually it would be great if it could be integrated with ASP.NET 2 somehow.
I mean I want to "simulate" a button click and there might be several buttons on the page doing different stuff.

rcastagna

Date:: Dec 21, 2006

Time:: 09:27

David,

Sometimes, this is the default behavior when the enter key is pressed...it depends on whether or not you a single "submit" button on a page (this includes usercontrols, third party controls, etc.).

I think there are a couple of ways you could go about simulating this behavior - both of them being client-side, however.

1) Wire up a global javascript event that "traps" the keypress event for the enter key, or

2) Add a javascript function which will conditionally process the events regardless of which control initiated the submit, ie:

<script type='text/javascript'>
function pressedEnter(control)
{
// You would want to put a switch statement in here
// to handle the control coming in.
alert(control);
}
</script>

<input type='text'>
<input type='text''>
<input type="submit" onsubmit='pressedEnter(this)'>

Granted, it's not a server-side solution, but really, none of the events are truly server-side...they're just client-side events that initiate specific server-side routines based on the information contained in the __doPostBack javascript function.

multani.sarbjit

Date:: Dec 28, 2006

Time:: 00:54

Well David,

Try this <form id="form1" defaultbutton="btnSubmit" defaultfocus="textbox1" runat="server">

This will set btnSubmit as default button of the form

david

Date:: Dec 28, 2006

Time:: 06:29

Thanks for your help guys.
I ended up doing it this way serverside (ASP.NET 2 / C#):

string loginFocusScript = "<script>var textBox = document.getElementById('" + this.Login.UsernameTextbox.ClientID + "');";
loginFocusScript += "if (textBox != null) {textBox.focus();}</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "loginFocus", loginFocusScript);

Not too happy with it, but it works.

Question Answered

This question has been closed, and points have been rewarded to the following experts:


rcastagna: 50

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es