Active Server Pages Programming (ASP)/Move focus by pressing enter key in ASP.NET 2.0 Web Form
Expert: Srini Nagarajan - 2/11/2008
QuestionHi
This is Yogesh and while searching on net I came across a question posted here
http://en.allexperts.com/q/Active-Server-Pages-1452/Pressing-Enter-Key.htm
Solution is also there posted by you
I tried using it in my ASP.Net 2.0 web form text boxes but my focus never gets shifted to next text box. Also whenever I say autopostback=true it submits it to server and don’t move the focus. Where should i put this script. In page load, Java Script etc.
If you have got any solution to this problem, Can you please send the sample code or some snippet?
Thanks and regards
Answerhi
You need to handle thru javascript
give a try something like this
txtBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + txtBox2.UniqueID + "').focus();return false;}} else {return true}; ");
txtBox2.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + txtBox3.UniqueID + "').focus();return false;}} else {return true}; ");
etc...
Not tested may need some tweaks..
-Srini