我有一个Login.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时,页面login.aspx将重新加载。 如何解决?我需要按下键盘上的ENTER与按下组件上的登录按钮具有相同的行为。
答案 0 :(得分:2)
我认为这可以使用Panel
包装器和DefaultButton
属性来实现。您需要将默认按钮设置为<LoginControlId>$LoginButton
,如下所示:
<asp:Panel runat="server" DefaultButton="MainLogin$LoginButton">
<asp:Login ID="MainLogin" runat="server" onloggingin="MainLogin_LoggingIn">
</asp:Login>
</asp:Panel>