One way to solve the problem is create your own javascript event handler to handle the keypress (or keydown if you want to), and generate the javascript to do the postback when keycode=13 either by calling button.click(this is the one I chose to use).
See the code below, you can copy to Notepad and save as .aspx, notice the idea is you call SetDefaultButton in Page_Load.
Also be aware special checks should be done when the active element is a button or a link since the expected behavior when pressing enter would be not going to the default button but firing the one with the focus (this could be very easily added by checking with the event.srcElement.tagName), but I wanted to keep the sample simple.
Thanks
Federico
<%@ Page language="c#" %>
<script runat=server>
private void Page_Load(object sender, System.EventArgs e) {
AspHelper.SetDefaultButton(this, login.ClientID + "$Button");
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button1"></asp:Button>
<asp:Button id="Button2" runat="server" Text="Button2"></asp:Button>
<asp:login runat=server id="login" />
</form>
</body>
</html>
<script runat=server>
class AspHelper {
public static void SetDefaultButton(Page page, Button button) {
SetDefaultButton(page, button.ClientID);
}
public static void SetDefaultButton(Page page, string clientID) {
if (!page.IsClientScriptBlockRegistered("MyFocus"))
page.RegisterClientScriptBlock("MyFocus","<script>" +
"var defaultButton=null;function document.onkeypress(){if (defaultButton==null || window.event.keyCode!=13) return;" +
"defaultButton.click();window.event.returnValue = false;return false;}</" + "script>");
page.RegisterStartupScript("setDefault", "<script>defaultButton = document.all['" + clientID + "'];</" + "script>");
}
}
</script>