Repeater动态增加行,PostBack保留数据
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btnNew" runat="server" Text="新建" onclick="btnNew_OnClick" /> <asp:Repeater ID="rpCustomerInfo" runat="server" onitemcommand="rpCustomerInfo_ItemCommand"> <HeaderTemplate> <table> <tr> <th>ID</th><th>Name</th><th>Telephone</th><th>RegisterDate</th><th>删除</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("ID")%>' ></asp:TextBox></td> <td><asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Name")%>' ></asp:TextBox></td> <td><asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("Telephone")%>' ></asp:TextBox></td> <td><asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("RegisterDate")%>' ></asp:TextBox></td> <td> <asp:LinkButton ID="lbnDelete" runat="server" CommandName="Delete" >删除</asp:LinkButton> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> </form> </body> </html>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace WebApplication1 { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { InitData(); DataBinds(); } } protected void InitData() { DateTime time = DateTime.Now; DataTable dt = new DataTable(); dt.Columns.Add("ID",typeof(string)); dt.Columns.Add("Name",typeof(string)); dt.Columns.Add("Telephone",typeof(string)); dt.Columns.Add("RegisterDate", typeof(string)); for (int i = 0; i < 2; i++) { DataRow dr=dt.NewRow(); dr["ID"] = i + 1; dr["Name"] = "user" + (i + 1); dr["Telephone"] = "123456"; dr["RegisterDate"] =time.AddDays(i * 10).ToString("yyyy-MM-dd"); dt.Rows.Add(dr); } ViewState.Add("Data",dt); } protected void DataBinds() { rpCustomerInfo.DataSource=ViewState["Data"] as DataTable; rpCustomerInfo.DataBind(); } protected DataTable CopyFormData() { DataTable dt = (ViewState["Data"] as DataTable).Clone(); foreach (RepeaterItem ri in rpCustomerInfo.Items) { DataRow dr = dt.NewRow(); dr["ID"]=(ri.FindControl("TextBox1") as TextBox).Text; dr["Name"] = (ri.FindControl("TextBox2") as TextBox).Text; dr["Telephone"] = (ri.FindControl("TextBox3") as TextBox).Text; dr["RegisterDate"] = (ri.FindControl("TextBox4") as TextBox).Text; dt.Rows.Add(dr); } return dt; } protected void btnNew_OnClick(object sender, EventArgs e) { DataTable dt = CopyFormData(); DataRow dr = dt.NewRow(); dt.Rows.Add(dr); ViewState.Add("Data",dt); DataBinds(); } protected void rpCustomerInfo_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName=="Delete") { DataTable dt = CopyFormData(); dt.Rows.RemoveAt(e.Item.ItemIndex); ViewState.Add("Data", dt); DataBinds(); } } } }
在ViewState中只需要保留这个DataTable的结构clone就可以,而不需要保存数据,因为每次回发到服务端, 不管是添加还是删除,我们都需要遍历这个repeater,然后重新赋值给这个Datatable.