Thursday, November 20, 2008

Use the ENTER Key In a TextBox

In most cases you'll want to let the users on the site to click ENTER in your Textbox,
and that will the trigger the same action as if they pressed the button next to the Textbox.

Paste that code on Page_Load.

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}; ");

the only 2 parameters that you need to change are:

TextBox1 = your text box.
Button1 = this is the button next to your text box.

So here is what it does bassicly:

It adds to the TextBox an attribute that says:
Add an event OnKeyDown (when you press your keyboard)
then it checks if I pressed on ENTER (which is 13 in ASCI)
and finally it adds the event that our button has which is CLICK to the Textbox ENTER key.

Hope that was helpful

I tried to make it as simple as I could.


No comments: