控件如何通过按 Enter 构建用户登录的方式

本文关键字:构建 Enter 用户 登录 方式 何通过 asp Login 控件 | 更新日期: 2023-09-27 18:33:13

可能的重复项:
当我按回车键时提交登录控制按钮

我有一个登录.aspx页面,其中包含:

<asp:Login ID="MainLogin" runat="server" onloggingin="MainLogin_LoggingIn"></asp:Login>

代码隐藏:

protected void MainLogin_LoggingIn(object sender, LoginCancelEventArgs e)
{                    
   SqlConnection myConnection = new SqlConnection(DBConnection.GetConnectionString());
   SqlCommand myCommand = new SqlCommand("SELECT * FROM Users WHERE login=@login and pass=@pass", myConnection);
   myCommand.Parameters.AddWithValue("login", MainLogin.UserName);
   myCommand.Parameters.AddWithValue("pass", MainLogin.Password);
   myCommand.Connection.Open();
   SqlDataReader Reader = myCommand.ExecuteReader();
   while (Reader.Read())
   {
      Session["curUserRole"] = Reader["role"].ToString();
      Session["curUserLogin"] = MainLogin.UserName;                    
      FormsAuthentication.RedirectFromLoginPage(MainLogin.UserName, MainLogin.RememberMeSet);                
      return;
   }
   //Reader.Close();
   //myCommand.Connection.Close();
   //myConnection.Close();                                              
}

用户键入他的登录名和密码,然后按组件的"登录"按钮进行登录。当用户键入其登录名和密码,然后按键盘上的 Enter 时,页面登录.aspx将重新加载。如何解决?我需要在键盘上按 ENTER 与按组件上的登录按钮具有相同的行为。

<asp:Login /> 控件如何通过按 Enter 构建用户登录的方式

我认为这可以使用Panel包装器与DefaultButton属性一起实现。您需要将默认按钮设置为 <LoginControlId>$LoginButton ,如下所示:

<asp:Panel runat="server" DefaultButton="MainLogin$LoginButton">
    <asp:Login ID="MainLogin" runat="server" onloggingin="MainLogin_LoggingIn">
    </asp:Login>
</asp:Panel>