article

Enter Key in ASP.NET

Email
Submitted on: 1/3/2015 10:36:00 PM
By: Richard Bean (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C#, VB.NET, ASP.NET
Views: 2282
 
     This article is solving very common problem when ASP.NET developers wants to get button "clicked" and submit a form when web site visitor hit an Enter key. That is useful when you want to build Login screen, web site search, pool etc.

 
				You can have only one text box and one button on a web form, but it is possible to have a lot of those elements (e.g. you can have on the same page web site search, login and pool).
We need to specify exactly what button will be "cliked" when visitor press Enter key, according to what textbox currently has a focus. The solution could be to add onkeydown attribute to textbox control:
 TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+Button1.UniqueID+"').click();return false;}} else {return true}; ");
This line of code will cause that button Button1 will be "clicked" when visitor press Enter key and cursor is placed in TextBox1 textbox. 
On this way you can "connect" as many text boxes and buttons as you want. 
Full research of Enter key problem in ASP.NET, including ASP.NET 2.0 you can see on http://www.beansoftware.com/ASP.NET-Tutorials/Accept-Enter-Key.aspx


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.