Tuesday, November 4, 2008

Fire a button click from any text box when enter key is pressed

If you have more than one button on a form ASP.Net doesn't know which button should be the default button.

With this code you may trigger any button click from any text box when the enter key is pressed.

TextBox.Attributes.Add("onkeydown", "javascript:if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)){document.getElementById('" & Button.ClientID & "').click();return false;}else return true;")

other way is if you are using panels then you can use the property
"Default Button".