日期:2014-05-18  浏览次数:20819 次

asp.net C#语言if else问题
为什么if 里面跳转的页面是Main2.asp而不是Main.asp

string username = this.name.SelectedValue.Trim();
  string userpassword =password.Text ;
  User user = new User(username, userpassword);
  if (username.Equals("admin")&& userpassword.Equals("123"))
  {
  Response.Redirect("Main.aspx");
  }
   
  else if (user.checkLogin())
  {
  Response.Redirect("Main2.aspx");
  }
  else
  {
  Response.Write("输入有误,请重新输入!");
  }


checkLogin方法
public bool checkLogin()
  {
  Database db = DatabaseFactory.CreateDatabase();
  string strSql = "select * from users where userName='" + name + "'and userPassword='" + password + "'";
  DbCommand cmd = db.GetSqlStringCommand(strSql);
  IDataReader dataReader = db.ExecuteReader(cmd);
  if (dataReader.Read())
  return true;
  else
  return false;
  }

------解决方案--------------------
探讨

为什么if 里面跳转的页面是Main2.asp而不是Main.asp?

------解决方案--------------------
if (username.Equals("admin")&& userpassword.Equals("123"))
=>
if (username == "admin" && userpassword == "123")

------解决方案--------------------
Equals重载过,应该等于==吧?

楼主设置断点跟踪一下就知道了,