protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] == null)
{
panelLogin.Visible = true;
labHuangYing.Text = "你好,请登陆!";
panelXinXi.Visible = false;
}
else
{
panelLogin.Visible = false;
string userinfo = Session["UserName"].ToString();
labHuangYing.Text = userinfo;
panelXinXi.Visible = true;
}
}
protected void btnDengLu_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["zyconnection"].ConnectionString); conn.Open(); SqlCommand CMD = new SqlCommand("select count (*) from tb_user where typeid=1 and username='"+txtUserName.Text+"' and userpassword='"+txtPassWord.Text+"'", conn); int count = Convert.ToInt32(CMD.ExecuteScalar()); if (txtUserName.Text == "" || txtPassWord.Text == "")
{
Response.Write("<script>alert('请输入用户名或密码')</script>");
}
else
{
if (count == 0)
{
Response.Write("<script>alert('用户名或密码错误!')</script>");
}
else
{
Session["UserName"] = txtUserName.Text;
Response.Redirect("Default.aspx");
conn.Close();
}
{
if (Session["UserName"] == null)
{
panelLogin.Visible = true;
labHuangYing.Text = "你好,请登陆!";
panelXinXi.Visible = false;
}
else
{
panelLogin.Visible = false;
string userinfo = Session["UserName"].ToString();
labHuangYing.Text = userinfo;
panelXinXi.Visible = true;
}
}
protected void btnDengLu_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["zyconnection"].ConnectionString); conn.Open(); SqlCommand CMD = new SqlCommand("select count (*) from tb_user where typeid=1 and username='"+txtUserName.Text+"' and userpassword='"+txtPassWord.Text+"'", conn); int count = Convert.ToInt32(CMD.ExecuteScalar()); if (txtUserName.Text == "" || txtPassWord.Text == "")
{
Response.Write("<script>alert('请输入用户名或密码')</script>");
}
else
{
if (count == 0)
{
Response.Write("<script>alert('用户名或密码错误!')</script>");
}
else
{
Session["UserName"] = txtUserName.Text;
Response.Redirect("Default.aspx");
conn.Close();
}
解决方案 »
- 昨天看了大家给的示例代码, 还是没解决问题. 只好上代码了,请指教.
- 只要一修改源文件再生成就提示我失败
- 谁能告诉我怎样设计C#窗体
- 点左侧Treeview中的不同项,右侧页面显示不同内容,该怎么实现?
- 请教一下,我在gridview控件里放了一个按钮,这个按钮是编辑的作用,但是要怎样才能在这个控件里取得主键值呢?
- 用sqlcommand作的更改如何提交到数据库
- C#调用C的dll的CString的问题
- .net2005中怎麼單擊dataGridView1一行時讓一整行得到焦點??????
- WPF,能解释下这个Shape形状吗?
- 关于启用XP样式的问题
- vs工具箱控件不可用 是怎么回事
- SourceGear Vault怎么用的?
protected void Page_Load(object sender, EventArgs e) --初始化页面
{
if (Session["UserName"] == null) 如果Session等于null了 提示登陆
{
panelLogin.Visible = true;
labHuangYing.Text = "你好,请登陆!";
panelXinXi.Visible = false;
}
else
{
panelLogin.Visible = false;
string userinfo = Session["UserName"].ToString();
labHuangYing.Text = userinfo;
//如果session不等于空的时候 让labHuangYing.Text 控件显示
panelXinXi.Visible = true;
}
}
protected void btnDengLu_Click(object sender, EventArgs e) 你的登陆事件
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["zyconnection"].ConnectionString);
conn.Open();
数据库连接
SqlCommand CMD = new SqlCommand("select count (*) from tb_user where typeid=1 and username='"+txtUserName.Text+"' and userpassword='"+txtPassWord.Text+"'", conn); int count = Convert.ToInt32(CMD.ExecuteScalar()); 执行SQL语句 查是否有此用户 if (txtUserName.Text == "" ¦ ¦ txtPassWord.Text == "") //验证用户控件是否为空(为什么要写在这里呢)
{
Response.Write(" <script>alert('请输入用户名或密码') </script>");
}
else
{
if (count == 0) //判断用户是否存在
{
//不存在提示用户名称或密码错误
Response.Write(" <script>alert('用户名或密码错误!') </script>");
}
else
{
存在的时候你把用户的UserName 保存给Session 然后转页 这里并没有体现你把Session显示在那里啊?
Session["UserName"] = txtUserName.Text;
Response.Redirect("Default.aspx");
conn.Close();
}
是你登陸的時候觸發的,
protected void btnDengLu_Click
是你Click的時候觸發的,
但是你顯示是在protected void Page_Load裡面,Click后又沒調用Load,怎么會顯示呢?
如樓上。
你这段程序
永远都赋值不到Session里