在页面上添加两个TextBox控件,分别命名为UserName和Email,其中UserName控件不允许修改,还有一些验证控件需要看情况使用,比如邮箱不能为空,邮箱的格式都需要验证
private
int
nUserID
=-
1
;
protected void Page_Load( object sender,EventArgs e)
... {
//从地址栏中获取nUserID的值
if(Resquest.Params["UserID"]!=null)
...{
//获取nUserID的值,并把该值转换为一个整数,即用户的ID值(UserID),如果在转换过程中失败,则页面中止初始化
if(Int32.TryParse(Resquest.Params["UserID"].ToString(),out nUserID)==false)
...{return;}
}
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())
...{
//读取数据
Email.Text=dr["Email"].ToString();
UserName.Text=dr["UserName"].ToString();
}
//关闭数据源
dr.Close();
}
protected void Page_Load( object sender,EventArgs e)
... {
//从地址栏中获取nUserID的值
if(Resquest.Params["UserID"]!=null)
...{
//获取nUserID的值,并把该值转换为一个整数,即用户的ID值(UserID),如果在转换过程中失败,则页面中止初始化
if(Int32.TryParse(Resquest.Params["UserID"].ToString(),out nUserID)==false)
...{return;}
}
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())
...{
//读取数据
Email.Text=dr["Email"].ToString();
UserName.Text=dr["UserName"].ToString();
}
//关闭数据源
dr.Close();
}
//当点击“修改”按钮时
protected
void
UpdateBtn_Click(
object
sender,EventArgs e)
... {
if(Page.IsValid)//通过验证控件的验证
...{
IUser user=new User();
try
...{
user.UpdateUser(nUserID,Email.Text.Trim());
Response.Write("<script>alert('"+"更新数据成功,请妥善保管好数据!"+"');</script>");
}
catch(Exception ex)
...{
Response.Redirect("~/ErrorPage.aspx?ErrorMsg="+ex.Message+"&ErrorUrl="+Request.Url.ToString());
}
}
}
... {
if(Page.IsValid)//通过验证控件的验证
...{
IUser user=new User();
try
...{
user.UpdateUser(nUserID,Email.Text.Trim());
Response.Write("<script>alert('"+"更新数据成功,请妥善保管好数据!"+"');</script>");
}
catch(Exception ex)
...{
Response.Redirect("~/ErrorPage.aspx?ErrorMsg="+ex.Message+"&ErrorUrl="+Request.Url.ToString());
}
}
}