我尝试写了一个ASP.NET+SQL2005的网站,在做登录界面时我是先自定义了一个登录控件,其中登录按钮点击时间如下:
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM User_Info WHERE UserName=@un AND Password=@psd", conn);
cmd.Parameters.AddWithValue("@un", this.TextBox_UserName.Text);
cmd.Parameters.AddWithValue("@psd", this.TextBox_psd.Text);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows==true)
{
try
{
sdr.Read();
Session["UserID"] = sdr["UserID"];
Session["NickName"] = sdr["NickName"];
Session["Type"] = sdr["Type"];
Session["Login"] = true;
this.Label1.Text = "登录成功";
Response.AddHeader("Refresh", "0");
}
catch (Exception ex)
{
this.Label1.Text = ex.Message.ToString();
}
}
else
this.Label1.Text = "登录失败";
}
在主页中拖入该控件,然后在主页LOAD_PAGE事件中定义如下:
if (!IsPostBack)
{
MyLogin1.Visible = true;
this.Logined1.Visible = false;
}
else
{ if (Session["Login"].Equals(false))
{
MyLogin1.Visible = true;
this.Logined1.Visible = false;
}
else
{
this.MyLogin1.Visible = false;
this.Logined1.Visible = true;
}
}
注:MyLogin是我自定义的登录界面,Logined是我自定义的已登录界面。
然而在运行的时候点击一次按钮,只显示登录成功,再点一次的话才会出现已登录界面。请各位高手解释一下,或者还有没有比较好的实现方法,谢谢!!!!!
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM User_Info WHERE UserName=@un AND Password=@psd", conn);
cmd.Parameters.AddWithValue("@un", this.TextBox_UserName.Text);
cmd.Parameters.AddWithValue("@psd", this.TextBox_psd.Text);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows==true)
{
try
{
sdr.Read();
Session["UserID"] = sdr["UserID"];
Session["NickName"] = sdr["NickName"];
Session["Type"] = sdr["Type"];
Session["Login"] = true;
this.Label1.Text = "登录成功";
Response.AddHeader("Refresh", "0");
}
catch (Exception ex)
{
this.Label1.Text = ex.Message.ToString();
}
}
else
this.Label1.Text = "登录失败";
}
在主页中拖入该控件,然后在主页LOAD_PAGE事件中定义如下:
if (!IsPostBack)
{
MyLogin1.Visible = true;
this.Logined1.Visible = false;
}
else
{ if (Session["Login"].Equals(false))
{
MyLogin1.Visible = true;
this.Logined1.Visible = false;
}
else
{
this.MyLogin1.Visible = false;
this.Logined1.Visible = true;
}
}
注:MyLogin是我自定义的登录界面,Logined是我自定义的已登录界面。
然而在运行的时候点击一次按钮,只显示登录成功,再点一次的话才会出现已登录界面。请各位高手解释一下,或者还有没有比较好的实现方法,谢谢!!!!!
Page_Load先执行
不放在IsPostBack里
或登录成功后跳转其他页面
{
MyLogin1.Visible = true;
this.Logined1.Visible = false;
}
//else
//{
if (Session["Login"].Equals(false))
{
MyLogin1.Visible = true;
this.Logined1.Visible = false;
}
else
{
this.MyLogin1.Visible = false;
this.Logined1.Visible = true;
}
//}