ILearnable .Net

August 18, 2006

Textbox submit on enter

Filed under: Uncategorized — andreakn @ 00:02

In .net 1.1, getting a textbox to submit on enter pressed is easy,

Put in the textbox (we’ll call it UserPassword here) on the form (runat=”server”) and declare it in the codebehind with the same ID)

Have a submit button, either of type or on the form,
declare a variable in the codebehind with the same ID as the button or input and of the same type (Button or HtmlInputButton)

Wire the submit button up to do the actual submitting and then in Page_Init or Page_Load write this line of code (13 is the magical number for the enter key)

UserPassword.Attributes.Add(“onkeydown”, “if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById(‘”+BtnSubmit.UniqueID+”‘).click();return false;}} else {return true}; “);

and there you have it


1 Comment »

  1. Thanks, for at very simple and easy to use solution!

    Comment by Casper Stendal — October 18, 2007 @ 12:43 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: