前臺的html代碼: html
<asp:GridView ID="gv_Emplogin" runat="server" AutoGenerateColumns="False" onrowdeleting="gv_Emplogin_RowDeleting" onrowupdating="gv_Emplogin_RowUpdating" onrowediting="gv_Emplogin_RowEditing" onrowcancelingedit="gv_Emplogin_RowCancelingEdit"> <Columns> <asp:BoundField DataField="Emp_id" HeaderText="用戶號" ReadOnly="True" /> <asp:BoundField DataField="Emp_name" HeaderText="登陸名" /> <asp:BoundField DataField="Password" HeaderText="密碼" /> <asp:BoundField DataField="name" HeaderText="姓名" /> <asp:BoundField DataField="Email" HeaderText="Email" /> <asp:BoundField DataField="Jb" HeaderText="用戶類型" /> <asp:BoundField DataField="Tell" HeaderText="聯繫電話" /> <asp:CommandField ShowEditButton="True" /> //編輯 <asp:CommandField ShowDeleteButton="True" /> //刪除 </Columns> </asp:GridView>
後臺代碼: this
/// <summary> /// GridView的綁定 /// </summary> public void Emplogin_Bind() { this.gv_Emplogin.DataSource = em.EmploginInfo(); this.gv_Emplogin.DataBind(); } /// <summary> /// GridView的刪除事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gv_Emplogin_RowDeleting(object sender, GridViewDeleteEventArgs e) { int Emp_id=int.Parse(gv_Emplogin.Rows[e.RowIndex].Cells[0].Text); if (em.Del_EmploginInfo(Emp_id) == 1) { Emplogin_Bind(); } } /// <summary> /// GridView的編輯事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gv_Emplogin_RowEditing(object sender, GridViewEditEventArgs e) { gv_Emplogin.EditIndex=e.NewEditIndex; } /// <summary> /// GridView的更新事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gv_Emplogin_RowUpdating(object sender, GridViewUpdateEventArgs e) { int Emp_id=int.Parse(gv_Emplogin.Rows[e.RowIndex].Cells[0].Text); EmployeeInfo ei = new EmployeeInfo(); ei.Emp_name = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim(); ei.Pwd = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim(); ei.Name = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim(); ei.Email = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim(); ei.Jb = int.Parse(((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim()); ei.Tell = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim(); if (em.Update_EmploginInfo(ei,Emp_id) == 1) { gv_Emplogin.EditIndex = -1; Emplogin_Bind(); } } /// <summary> /// GridView取消編輯事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gv_Emplogin_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gv_Emplogin.EditIndex = -1; Emplogin_Bind(); }