Friday, September 5, 2008

Handle ENTER key in web page


//Assign enter button in code behind

txtAmtFrom.Attributes.Add("onkeydown", "clickHandler(document.all." + btnSearch.ClientID + ")");
txtAmtTo.Attributes.Add("onkeydown", "clickHandler(document.all." + btnSearch.ClientID + ")");
// ...

// here the "btnSearch" is the button control id whose click event will be fired

// the handler
function clickHandler(btn)
{
if (event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
btn.click();
}
}

No comments: