Allow Enter key to login in asp.net?

后端 未结 4 1225
南笙
南笙 2021-01-02 04:51

I have a standard asp:login control:



        
相关标签:
4条回答
  • 2021-01-02 05:10

    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;
        }
    
    0 讨论(0)
  • 2021-01-02 05:11
    Button lbButton = Login1.FindControl("LoginButton") as Button;
    
    ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
    contentPlaceHolder.Page.Form.DefaultButton = lbButton.UniqueID;
    
    0 讨论(0)
  • 2021-01-02 05:29

    I know this is a super old post, but another way to do this is by using an asp:Panel with DefaultButton set to the ID of the button that the user would normally click on to log in:

    <asp:Login ID="LoginUser" runat="server">
      <LayoutTemplate>
        <asp:Panel ID="LoginPanel" runat="server" DefaultButton="LoginButton">
          <other stuff here like username and password textboxes>
          <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In"/>
      </asp:Panel>
    </LayoutTemplate>
    

    0 讨论(0)
  • 2021-01-02 05:31

    This is a hack, but it will provide a work around for your issue with Internet Explorer.
    Add a text box to your page that is hidden from view.

    <!-- Hack for Internet Explorer browsers to allow the page to post back when the enter key is pressed-->
    <asp:TextBox ID="txtIEHackBox" runat="server" style="visibility: hidden; display: none;" />
    

    This will cause Internet Explorer to send back the Button Web control's name/value pair upon hitting Enter.

    0 讨论(0)
提交回复
热议问题