各位大侠,我是菜鸟,小弟有问题问,着急啊。我做了个很小网站,有几个板块,只有登录才能浏览,一下有简单代码,但是不成功,不管是否登录,都不能浏览,大侠帮我看看啊!!谢谢
点击“登录”:
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=localhost;Initial Catalog=电影信息交流系统;Integrated Security=True"; try
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
if ("会员" == DropDownList1.Text)
{
cmd.CommandText = "select * from 用户信息表 where 注册用户名 = '" + TextBox1.Text + "'and 密码='" + TextBox2.Text + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//登陆成功
Session["rename"] = "ok"; /////////////////////////登录成功,设置成ok!!
Response.Redirect("欢迎会员.aspx");
}
else
{
//登陆不成功
Response.Write("<script>alert('错误!请重新输入用户名或密码!!')</script>");
}
dr.Close();
}
else if ("管理员" == DropDownList1.Text)
{
cmd.CommandText = "select * from 管理员表 where 管理员用户名 = '" + TextBox1.Text + "'and 密码='" + TextBox2.Text + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//登陆成功
Session["rename"] = TextBox1.Text;
Response.Redirect("欢迎管理员.aspx");
}
else
{
//登陆不成功
// MessageBox.Show("错误!请重新输入用户名或密码!");
Response.Write("<script>alert('错误!请重新输入用户名或密码!!')</script>");
}
dr.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message); } cn.Close();
}
在某个板块:(我希望登录才能浏览)在主页中点击一个hyperlink进入.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["rename"] == null || !Session["rename"].Equals("true"))
{
Response.Redirect("loginFail.aspx");
}
else
{
Response.Redirect("影视档案.aspx");
}
}
点击“登录”:
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=localhost;Initial Catalog=电影信息交流系统;Integrated Security=True"; try
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
if ("会员" == DropDownList1.Text)
{
cmd.CommandText = "select * from 用户信息表 where 注册用户名 = '" + TextBox1.Text + "'and 密码='" + TextBox2.Text + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//登陆成功
Session["rename"] = "ok"; /////////////////////////登录成功,设置成ok!!
Response.Redirect("欢迎会员.aspx");
}
else
{
//登陆不成功
Response.Write("<script>alert('错误!请重新输入用户名或密码!!')</script>");
}
dr.Close();
}
else if ("管理员" == DropDownList1.Text)
{
cmd.CommandText = "select * from 管理员表 where 管理员用户名 = '" + TextBox1.Text + "'and 密码='" + TextBox2.Text + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//登陆成功
Session["rename"] = TextBox1.Text;
Response.Redirect("欢迎管理员.aspx");
}
else
{
//登陆不成功
// MessageBox.Show("错误!请重新输入用户名或密码!");
Response.Write("<script>alert('错误!请重新输入用户名或密码!!')</script>");
}
dr.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message); } cn.Close();
}
在某个板块:(我希望登录才能浏览)在主页中点击一个hyperlink进入.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["rename"] == null || !Session["rename"].Equals("true"))
{
Response.Redirect("loginFail.aspx");
}
else
{
Response.Redirect("影视档案.aspx");
}
}
一开始没有赋值的时候是NULL你后来赋值了不是OK吗?
nul就可以了,还有啊,你测试完了关闭浏览器啊,下次要从新打开,要不就会session,是你上次的username