GridView实现数据编辑和删除
< asp:GridView ID = "gv_Emplogin" runat = "server" AutoGenerateColumns = "False" |
02 |
onrowdeleting = "gv_Emplogin_RowDeleting" |
03 |
onrowupdating = "gv_Emplogin_RowUpdating" |
04 |
onrowediting = "gv_Emplogin_RowEditing" |
05 |
onrowcancelingedit = "gv_Emplogin_RowCancelingEdit" > |
06 |
< Columns > |
07 |
< asp:BoundField DataField = "Emp_id" HeaderText = "用户号" ReadOnly = "True" /> |
08 |
< asp:BoundField DataField = "Emp_name" HeaderText = "登录名" /> |
09 |
< asp:BoundField DataField = "Password" HeaderText = "密码" /> |
10 |
< asp:BoundField DataField = "name" HeaderText = "姓名" /> |
11 |
< asp:BoundField DataField = "Email" HeaderText = "Email" /> |
12 |
< asp:BoundField DataField = "Jb" HeaderText = "用户类型" /> |
13 |
< asp:BoundField DataField = "Tell" HeaderText = "联系电话" /> |
14 |
|
15 |
< asp:CommandField ShowEditButton = "True" /> //编辑 |
16 |
< asp:CommandField ShowDeleteButton = "True" /> //删除 |
17 |
</ Columns > |
18 |
|
19 |
</ asp:GridView > |
后台代码:
01 |
/// <summary> |
02 |
/// GridView的绑定 |
03 |
/// </summary> |
04 |
public void Emplogin_Bind() |
05 |
{ |
06 |
this.gv_Emplogin.DataSource = em.EmploginInfo(); |
07 |
this.gv_Emplogin.DataBind(); |
08 |
} |
09 |
10 |
/// <summary> |
11 |
/// GridView的删除事件 |
12 |
/// </summary> |
13 |
/// <param name= "sender" ></param> |
14 |
/// <param name= "e" ></param> |
15 |
protected void gv_Emplogin_RowDeleting(object sender, GridViewDeleteEventArgs e) |
16 |
{ |
17 |
int Emp_id=int.Parse(gv_Emplogin.Rows[e.RowIndex].Cells[0].Text); |
18 |
if (em.Del_EmploginInfo(Emp_id) == 1) |
19 |
{ |
20 |
Emplogin_Bind(); |
21 |
} |
22 |
} |
23 |
24 |
/// <summary> |
25 |
/// GridView的编辑事件 |
26 |
/// </summary> |
27 |
/// <param name= "sender" ></param> |
28 |
/// <param name= "e" ></param> |
29 |
protected void gv_Emplogin_RowEditing(object sender, GridViewEditEventArgs e) |
30 |
{ |
31 |
gv_Emplogin.EditIndex=e.NewEditIndex; |
32 |
|
33 |
} |
34 |
35 |
/// <summary> |
36 |
/// GridView的更新事件 |
37 |
/// </summary> |
38 |
/// <param name= "sender" ></param> |
39 |
/// <param name= "e" ></param> |
40 |
protected void gv_Emplogin_RowUpdating(object sender, GridViewUpdateEventArgs e) |
41 |
{ |
42 |
int Emp_id=int.Parse(gv_Emplogin.Rows[e.RowIndex].Cells[0].Text); |
43 |
EmployeeInfo ei = new EmployeeInfo(); |
44 |
ei.Emp_name = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim(); |
45 |
ei.Pwd = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim(); |
46 |
ei.Name = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim(); |
47 |
ei.Email = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim(); |
48 |
ei.Jb = int.Parse(((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim()); |
49 |
ei.Tell = ((TextBox)(gv_Emplogin.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim(); |
50 |
if (em.Update_EmploginInfo(ei,Emp_id) == 1) |
51 |
{ |
52 |
gv_Emplogin.EditIndex = -1; |
53 |
Emplogin_Bind(); |
54 |
} |
55 |
} |
56 |
57 |
/// <summary> |
58 |
/// GridView取消编辑事件 |
59 |
/// </summary> |
60 |
/// <param name= "sender" ></param> |
61 |
/// <param name= "e" ></param> |
62 |
protected void gv_Emplogin_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) |
63 |
{ |
64 |
gv_Emplogin.EditIndex = -1; |
65 |
Emplogin_Bind(); |
66 |
} |