Capture Enter Key on ASP.NET TextBox

Here is a method you can pass a text box and a command button to add the onpress event logic.

namespace SURFTHRU
{
    public static class HTMLControls
    {
        public static void SetEnterButton(TextBox txtBox, Button btn)
        {
            txtBox.Attributes.Add("onkeydown",
                      "if(event.which || event.keyCode) { if ((event.which == 13) || (event.keyCode == 13)) " +
                      "{document.getElementById('" + btn.ClientID + "').click(); return false;}}" +
                      "else {return true}; ");
        }

    }
}
Comments are closed