Default ASP.NET Button

In my previous life as an ASP.NET ninja, I had a request from a client to use the enter button to submit a form. It turns out to be a little bit tricky until you find the magic code. Today I was the same question and found the answer after a quick Live Search.

EmailTextBox.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + LoginButton.UniqueID + "','')");

This adds some javascript to your textbox which performs a postback on a specific button when the enter key is pressed in the text box. If you're interested in the ASPNET2.0 way of doing this, check out Scott Guthrie's comment on that post.

Skip to main content