asp.net用户修改密码的页面功能如何设计
用Microsoft Visual Studio 2005开发工具开发asp.net网站时,经常会涉及到让用户修改原来的密码,那么asp.net用户修改密码的页面功能如何设计?(这里只分享一下后台代码的编写)
操作方法
- 01
protected void Page_Load(object sender, EventArgs e) { if (Session["userName"] == null || Session["userName"].ToString() == "") { Response.Redirect("login.aspx"); } else { string userName = Session["userName"].ToString();//定义变量接受用户登录传过来的值 this.lbl_userName.Text = userName; string userPwd = Session["userPassword"].ToString(); this.lbl_userPwd.Text = userPwd; } } protected void Button2_Click(object sender, EventArgs e) { string userPwd = this.lbl_userPwd.Text;//数据库中的密码(原密码) string oldPwd = this.tbx_oldPwd.Text;//用户填写的原密码 string newPwd1 = this.tbx_newPwd.Text;//用户填写的新密码 string newPwd2 = this.tbx_confirmnewPwd.Text;//用户填写的确认密码 if (newPwd1 == "" || newPwd2 == "") Response.Write("<script>alert('密码不能为空')</script>"); else { if (oldPwd != userPwd) { Response.Write("<script>alert('密码错误,请重新输入')</script>"); } else { if (!newPwd1.Equals(newPwd2)) { Response.Write("<script>alert(\"两次密码不一致!请重新输入!\");</script>"); } else { SqlConnection conn = new SqlConnection("server=.;database=taoke;uid=sa;pwd=123456"); SqlCommand cmd = new SqlCommand(); string sql = "update users set userPwd='" + newPwd1 + "' where userName='" + this.lbl_userName.Text + "'"; cmd.CommandText = sql; cmd.Connection = conn; conn.Open(); int i = (int)cmd.ExecuteNonQuery(); if (i > 0) { Response.Write("<script>alert(\"密码修改成功!,必须重新登录\");</script>"); Server.Transfer("login.aspx"); } else { Response.Write("<script>alert(\"更新失败!\");</script>"); } conn.Close(); }