七.修改用户密码 EditPwd.aspx

来源:互联网 发布:seo研究中心微博 编辑:程序博客网 时间:2024/06/05 17:56

在页面添加4个TextBox控件,分别命名为UserName,OldPassword,NewPassword和PasswordStr(确认新密码),还有相应的验证控件

//首先判断用户是否登录,如果没有登录,则跳转到用户登录页面Default.aspx

 

private int nUserID=-1;

protected void Page_Load(object sender,EventArgs e)
{   
   
if(Session["UserID"]!=null)
    
{
      
//获取nUserID的值,并把该值转换为一个整数,即用户的ID值(UserID),如果在转换过程中失败,则页面中止初始化
       if(Int32.TryParse(Session["UserID"].ToString(),out nUserID)==false)
        
{return;}
    }

    
else
    
{
       Response.Redirect(
"~/Default.aspx");
    }

   
if(!Page.IsPostBack)
    
{
      
if(nUserID>-1)
        
{
          BindUserData(nUserID);
//从数据库中读取数据,并显示
        }

      
else{UpdateBtn.Enabled=false;}
    }

}


private void BindUserData(int nUserID)
{
  IUser user
=new User();
  SqlDataReader dr
=user.GetSingleUser(nUserID);
  
if(dr.Read())
   
{
      
//读取数据      
      UserName.Text=dr["UserName"].ToString();
   }

  
//关闭数据源
  dr.Close();
}



protected void UpdateBtn_Click(object sender,EventArgs e)
{
   
if(Page.IsValid)//通过验证控件的验证
    {
       IUser user
=new User();
       SqlDataReader dr
=user.GetUserLoginByProc(UserName.Text.Trim(),OldPassword.Text.Trim());
       
//读取UserID的值
       string sUserID="";
       
if(dr.Read()){sUserID=dr["UserID"].ToString();}
       dr.Close();
       
//判断用户输入的旧密码是否正确
       if(sUserID==null||sUserID==""||sUserID.Length<0)
        
{
          Response.Write(
"<script>alert('"+"旧密码输入错误,请重新输入密码!"+"');</script>");
          
return;
        }
  


       
try
        
{
           user.UpdateUserPwd(nUserID,NewPassword.Text.Trim());
           Response.Write(
"<script>alert('"+"修改密码成功,请妥善保管好数据!"+"');</script>");
        }

       
catch(Exception ex)
        
{
           Response.Redirect(
"~/ErrorPage.aspx?ErrorMsg="+ex.Message+"&ErrorUrl="+Request.Url.ToString());
        }

    }

}


//点击“返回”按钮时
protected void ReturnBtn_Click(object sender,EventArgs e)
{
  Response.Redirect(
"~/UserManage.aspx");
}