use*_*637 1 .net asp.net login
我有登录页面的问题.如果我不输入用户名或密码,它应该说"请输入一个用户名或密码"而不是它去目的地页面我的意思是没有在登录字段输入任何东西如果我点击提交按钮它将欢迎页面实际上哪个不应该发生.
这是我的代码请有人告诉我我的错误在哪里:
public class Login
{
public string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
public int GetLogin(string UserName, string Password)
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select * from Login where UserName='"+UserName+"' and Password='"+Password+"'",con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
if ((ds.Tables[0].Rows[0].ItemArray[0].ToString() == UserName) && (ds.Tables[0].Rows[0].ItemArray[1].ToString() == Password))
{
return 1;
}
else
{
return 0;
}
}
else
{
return -1;
}
}
Login.aspx.cs:
protected void BtnLogin_Click(object sender, EventArgs e)
{
Session["UserName"] = TxtUserName.Text;
Login lg = new Login();
if ((lg.GetLogin(TxtUserName.Text, TxtPassword.Text) == 1))
{
Response.Redirect("c1.aspx");
}
else if((TxtUserName.Text=="")&&(TxtPassword.Text==""))
{
Lbl1.Text = "Please Enter the UserName and Password";
}
else
{
Lbl1.Text = "Sorry,Invalid UserName or Password";
}
}
Run Code Online (Sandbox Code Playgroud)
所有这些功能都已内置于ASP.NET中.
查看RequiredFieldValidator以验证您的字段,或使用Login Controls来处理所有表单功能.
查看FormsAuthentication和Membership以进行用户身份验证.