repeater编辑更新删除
1.
2. <asp:Repeater id="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">
3. <ItemTemplate>
4. <asp:Panel ID="plItem" runat="server">
5. <tr>
6. <td><%# DataBinder.Eval(Container.DataItem,"userid") %></td>
7. <td><%# DataBinder.Eval(Container.DataItem,"username") %></td>
8. <td>
9. <asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
10. CommandName="Edit" Text="编辑"></asp:LinkButton>
11. <asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
12. CommandName="Delete" Text="删除"></asp:LinkButton>
13. </td>
14. </tr>
15. </asp:Panel>
16.
17. <asp:Panel ID="plEdit" runat="server">
18. <tr>
19. <td><%# DataBinder.Eval(Container.DataItem,"userid") %></td>
20. <td><asp:TextBox ID="UserName" Text='<%# DataBinder.Eval(Container.DataItem,"username") %>' runat="server"></asp:TextBox></td>
21. <td>
22. <asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
23. CommandName="Update" Text="更新"></asp:LinkButton>
24. <asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
25. CommandName="Cancel" Text="取消"></asp:LinkButton>
26. </td>
27. </tr>
28. </asp:Panel>
29. </ItemTemplate>
30. </asp:Repeater>
31.
32.
33. public int id = 0;
34.
35. protected void Page_Load(object sender, EventArgs e)
36. {
37.
38. if (!IsPostBack)
39. {
40. BindGrid();
41. }
42.
43. }
44.
45. private void BindGrid()
46. {
47. string sql = "select top 5 * from [user] order by userid desc";
48.
49. OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("test.mdb"));
50.
51. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
52.
53. DataSet ds = new DataSet();
54.
55. da.Fill(ds, "temp");
56.
57. Repeater1.DataSource = ds.Tables["temp"];
58.
59. Repeater1.DataBind();
60.
61. }
62.
63.
64. protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
65. {
66.
67. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
68. {
69. DataRowView rowv = (DataRowView)e.Item.DataItem;
70. string userid = rowv["userid"].ToString();
71.
72. if (userid != id.ToString())
73. {
74. ((Panel)e.Item.FindControl("plItem")).Visible = true;
75. ((Panel)e.Item.FindControl("plEdit")).Visible = false;
76. }
77. else
78. {
79. ((Panel)e.Item.FindControl("plItem")).Visible = false;
80. ((Panel)e.Item.FindControl("plEdit")).Visible = true;
81. }
82.
83. }
84. }
85.
86.
87. protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
88. {
89. if (e.CommandName == "Edit")
90. {
91. id = int.Parse(e.CommandArgument.ToString());
92. }
93. else if (e.CommandName == "Cancel")
94. {
95. id = -1;
96. }
97. else if (e.CommandName == "Update")
98. {
99. //Update.
100.
101. string username = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("UserName")).Text.Trim();
102.
103. this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('更新ID:" + e.CommandArgument + ";页面值:姓名=" + username + "');", true);
104. }
105. else if (e.CommandName == "Delete")
106. {
107. //Delete.
108. this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
109. }
110.
111. BindGrid();
112.
113. }
114.
115.