Allow Enter key to login in asp.net?

10,079

In the designer of your Login control: "Convert To Template". Then in the Page Load set the defaultButton of your form by finding the LoginButton.

ASPX:

<form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
            <LayoutTemplate>
                <table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse;">
                    <tr>
                        <td>
                            <table border="0" cellpadding="0">
                                .....
                                <tr>
                                    <td align="right" colspan="2">
                                        <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:Login>
    </div>
    </form>

Code-Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        Button lbButton = Login1.FindControl("LoginButton") as Button;
        form1.DefaultButton = lbButton.UniqueID;
    }
Share:
10,079
DisgruntledGoat
Author by

DisgruntledGoat

I'm a web developer and programmer from the UK. I'll fill this out more when I can be bothered; really I'm just trying to get the autobiography badge.

Updated on July 20, 2022

Comments

  • DisgruntledGoat
    DisgruntledGoat almost 2 years

    I have a standard asp:login control:

    <asp:Login ID="mbLogin" runat="server" TitleText=""
      DestinationPageUrl="~/Default.aspx"
      PasswordRecoveryText="Forgot your password?"
      PasswordRecoveryUrl="~/LostPassword.aspx"></asp:Login>
    

    In Internet Explorer, pressing Enter does not submit the form, but IE beeps at me 10 times rapidly. In other browsers Enter works perfectly fine and submits the forum as you'd expect.

    I've seen this question but that only works when you have actual form element with an actual button, not the login control as a whole.

    Why is it being blocked in IE (and why 10 times for some reason)? Is there a workaround?

  • Styxxy
    Styxxy over 11 years
    Some explanation would be nice and also where to put it (for example in the Page_Load event).