[C#] 纯文本查看 复制代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindSelect();//这个会在下一篇专门介绍如何使用静态集合
DoUpdate();
}
}
public void DoUpdate()
{
//修改操作初始化
if (Request["id"] != null)
{
string id = Request["id"];
int userId = 0;
if (!int.TryParse(id, out userId))
{
return;
}
//通过userId获取当前LoginUserInfo对象和UserInfo对象
LoginUserInfo loginuser = loginbll.FindById(userId);
UserInfo user = userbll.FindById(userId);
if (loginuser == null || user == null)
{
return;
}
//以下是页面初始化操作
txtUserName.Value=user.Name;
ddlGender.Value=user.Sex.ToString();
txtPhone.Value=user.Phone;
ddlZodiac.Value=user.ZodiacId.ToString();
ddlProv.Value=user.ProvinceId.ToString();
ddlCity.Items.Clear();
GetOptions(ddlCity, Area.Dict[user.ProvinceId].Children.Values.ToList());
ddlCity.Value=user.CityId.ToString();
ddlConstellation.Value=user.ConstellationId.ToString();
ddlDegree.Value=user.DegreeId.ToString();
ddlNation.Value=user.NationId.ToString();
txtLoginUserName.Value=loginuser.UserName;
ddlUserStatus.Value = Convert.ToInt32(loginuser.UserStatus).ToString();
txtPwd.Value=loginuser.UserPwd;
ddlLoginType.Value = Convert.ToInt32(loginuser.LoginType).ToString();
}
}
protected void btnOk_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Update();
}
}
public void Update()
{
if (Request["id"] != null)
{
string id = Request["id"];
int userId = 0;
if (!int.TryParse(id, out userId))
{
return;
}
//从页面输入信息组织UserInfo对象
UserInfo user = GetUser();
user.ID = userId;
//从页面输入信息组织LoginUserInfo对象(传递唯一标识ID)
LoginUserInfo loginuser = GetLoginUser(userId);
//更新
loginbll.UpdateLoginUserInfo(loginuser);
//更新
userbll.UpdateUserInfo(user);
Response.Redirect("list.aspx");
}
}
/// <summary>
/// 获取UserInfo对象 通过页面 此时返回的对象ID未赋值 修改操作时需赋ID值
/// </summary>
/// <returns></returns>
public UserInfo GetUser()
{
string name = txtUserName.Value;
string phone = txtPhone.Value.Trim(); ;
string sex = ddlGender.Value;
string zodiac = ddlZodiac.Value;
string constellation = ddlConstellation.Value;
string degree = ddlDegree.Value;
string nation = ddlNation.Value;
string provice = ddlProv.Value;
string city = ddlCity.Value;
UserInfo user = new UserInfo
{
Name = name,
Phone = phone,
Sex = InputHelper.GetInputInt(sex),
ZodiacId = InputHelper.GetInputInt(zodiac),
ConstellationId = InputHelper.GetInputInt(constellation),
DegreeId = InputHelper.GetInputInt(degree),
NationId = InputHelper.GetInputInt(nation),
ProvinceId = InputHelper.GetInputInt(provice),
CityId = InputHelper.GetInputInt(city)
};
return user;
}
/// <summary>
/// 获取LoginUserInfo对象
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public LoginUserInfo GetLoginUser(int userId)
{
string loginUser = txtLoginUserName.Value;
string userStatus = ddlUserStatus.Value;
string pwd = txtPwd.Value;
string loginType = ddlLoginType.Value;
string loginIP = Request.UserHostAddress;
LoginUserInfo loginuser = new LoginUserInfo
{
ID = userId,
UserName = loginUser,
UserPwd = pwd,
LoginIp = loginIP,
UserStatus = (UserStatus)int.Parse(userStatus),
LoginType = (LoginType)int.Parse(loginType)
};
return loginuser;
}