【分布式系统框架教程】-修改一条数据
导读部分
教程部分
修改操作也很简单,相应的dall代码我就不再贴出来了,下面贴一下bll层的吧 [C#] 纯文本查看 复制代码 public class LoginUserBLL
{
LoginUserDAL dal = new LoginUserDAL();
/// <summary>
/// 更新对象
/// </summary>
/// <param name="info">LoginUserInfo对象</param>
/// <returns></returns>
public int UpdateLoginUserInfo(LoginUserInfo info)
{
return dal.UpdateLoginUserInfo(info);
}
}
}
[C#] 纯文本查看 复制代码 public class UserBLL
{
UserDAL dal=new UserDAL();
/// <summary>
/// 修改一条记录
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public int UpdateUserInfo(UserInfo user)
{
return dal.UpdateUserInfo(user);
}
}
下面是web前段代码: [C#] 纯文本查看 复制代码 protected void Page_Load(object sender, EventArgs e)[/align] {
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;
} 好了这就已经搞定了,效果图很简单我就不再贴了,无非就是修改成功跳转到列表这些常规的方式了。
|