我想做一个操作前必须先登录的效果 代码如下Login.aspx.cs
protected void _btnLogin_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=D:\\Visual Studio 2008\\WebSites\\4-13\\App_Data\\user.mdb");
conn.Open();
string sqlstr = "select * from [User] where username='" + _txtUserName.Text + "' and userpassword ='" + _txtUserPassword.Text + "'";
OleDbCommand cmd = new OleDbCommand(sqlstr, conn);
OleDbDataReader reader;
reader = cmd.ExecuteReader();
if (reader.Read())
{
this.Session["userName"] = this._txtUserName.Text;
this.Response.Redirect("Upload.aspx");
reader.Close();
}Upload.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (this.Session["userName"] != null)
{
this.Response.Redirect("Upload.aspx");
}
else {
this.Response.Write("<script>alert('请先登录');</script>");
this.Response.Redirect("Login.aspx");
}
}结果发现
问题1:输入正确的账号密码后不能跳转到Upload.aspx中
问题2:因为我还有其它页面也需要先登录才能操作,所以第一段代码中this.Response.Redirect("Upload.aspx")我是不想要的,我只是想登录后有一个session,这样我就可以在其他页面进行操作了。请问我应该怎么改上面的代码
protected void _btnLogin_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=D:\\Visual Studio 2008\\WebSites\\4-13\\App_Data\\user.mdb");
conn.Open();
string sqlstr = "select * from [User] where username='" + _txtUserName.Text + "' and userpassword ='" + _txtUserPassword.Text + "'";
OleDbCommand cmd = new OleDbCommand(sqlstr, conn);
OleDbDataReader reader;
reader = cmd.ExecuteReader();
if (reader.Read())
{
this.Session["userName"] = this._txtUserName.Text;
this.Response.Redirect("Upload.aspx");
reader.Close();
}Upload.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (this.Session["userName"] != null)
{
this.Response.Redirect("Upload.aspx");
}
else {
this.Response.Write("<script>alert('请先登录');</script>");
this.Response.Redirect("Login.aspx");
}
}结果发现
问题1:输入正确的账号密码后不能跳转到Upload.aspx中
问题2:因为我还有其它页面也需要先登录才能操作,所以第一段代码中this.Response.Redirect("Upload.aspx")我是不想要的,我只是想登录后有一个session,这样我就可以在其他页面进行操作了。请问我应该怎么改上面的代码
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货