GridView 的增删改查

it2023-06-09  76

using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using OASys.BLL; using OASys.Models; using System.Collections.Generic; public partial class _Default : System.Web.UI.Page {     public UserInfoManager info = new UserInfoManager();     /// <summary>     /// 窗体加载事件     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     protected void Page_Load(object sender, EventArgs e)     {         if (!IsPostBack)         {             LoadInfo();         }     }     /// <summary>     /// 加载用户信息的方法     /// </summary>     private void LoadInfo()     {         List<UserInfo> users =new List<UserInfo>();         foreach (UserInfo user in info.GetAllUserInfo())         {             if (user.Gender == 1)             {                 user.DepartId.sex = "男";             }             else             {                 user.DepartId.sex = "女";             }             users.Add(user);         }         gdvUserInfo.DataSource =users;         gdvUserInfo.DataBind();     }     /// <summary>     /// 分页事件     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     protected void gdvUserInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)     {         gdvUserInfo.PageIndex = e.NewPageIndex;         LoadInfo();     }     /// <summary>     /// 删除信息事件     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)     {         string id =gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();         if (info.DeleteUserInfoById(id) > 0)         {             this.lblMessage.Text = "删除成功!";             LoadInfo();         }         else         {             this.lblMessage.Text = "删除失败!";             LoadInfo();         }     }     /// <summary>     /// 编辑事件     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)     {         gdvUserInfo.EditIndex = e.NewEditIndex;         LoadInfo();     }     /// <summary>     /// 取消编辑事件     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     protected void gdvUserInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)     {         gdvUserInfo.EditIndex = -1;         LoadInfo();     }     /// <summary>     /// 更新事件     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)     {         string userId = gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();         TextBox txtUserId = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserId");         TextBox txtUserName = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserName");         TextBox txtPassWord = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtPassWord");         if (txtUserName != null && txtPassWord != null)         {             try             {                 UserInfo user = info.GetUserInfoById(userId);                 user.UserName = txtUserName.Text.ToString();                 user.Password = txtPassWord.Text.ToString();                 if (info.ModifyUserInfo(user) > 0)                 {                     this.lblMessage.Text = "修改成功!";                     gdvUserInfo.EditIndex = -1;                     LoadInfo();                 }                 else                 {                     this.lblMessage.Text = "修改失败!";                     gdvUserInfo.EditIndex = -1;                     LoadInfo();                 }             }             catch (Exception)             {                                 this.lblMessage.Text = "修改失败!";                     gdvUserInfo.EditIndex = -1;                     LoadInfo();             }         }     }     /// <summary>     ///      /// 光棒事件     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)     {         for (int i = 0; i < gdvUserInfo.Rows.Count; i++)         {             if (gdvUserInfo.Rows[i].RowType == DataControlRowType.DataRow)             {                 gdvUserInfo.Rows[i].Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#9cf'");                 gdvUserInfo.Rows[i].Attributes.Add("onmouseout", "this.style.backgroundColor=c");             }         }     } }

最新回复(0)