Enter Key in ASP.NET
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.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
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
Original Comments (3)
Recovered from Wayback Machine