文章参考:文章参考http://www.cnblogs.com/dataadapter/archive/2012/06/25/2562885.html
效果:
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="rptTest.Default" %>
后台代码:
添加一行:
protected void btnAddNewRow_Click(object sender, EventArgs e) { //首先,恢复数据源 DataTable dt = DefineDataTableSchema(hfRptColumns.Value); foreach (RepeaterItem item in rptTest.Items) { DataRow newRow = dt.NewRow(); newRow["receiver"] = ((Label)item.FindControl("lblReceiver")).Text; newRow["expense_amount"] = ((TextBox)item.FindControl("txtExpenseAmount")).Text; newRow["cut_payment_amount"] = ((TextBox)item.FindControl("txtCutPaymentAmount")).Text; newRow["acutal_amount"] = ((Label)item.FindControl("lblAcutalAmount")).Text; newRow["bank_no"] = ((Label)item.FindControl("lblBankNo")).Text; newRow["bank_name"] = ((Label)item.FindControl("lblBankName")).Text; dt.Rows.Add(newRow); } //添加一行 DataRow row = dt.NewRow(); dt.Rows.Add(row); rptTest.DataSource = dt; rptTest.DataBind(); }
删除一行
protected void btnDel_Click(object sender, EventArgs e) { var arr = new System.Collections.ArrayList(); DataTable dt = DefineDataTableSchema(hfRptColumns.Value); foreach (RepeaterItem item in rptTest.Items) { var chk = item.FindControl("ckBox") as CheckBox; if (chk.Checked) continue; DataRow newRow = dt.NewRow(); newRow["receiver"] = ((Label)item.FindControl("lblReceiver")).Text; newRow["expense_amount"] = ((TextBox)item.FindControl("txtExpenseAmount")).Text; newRow["cut_payment_amount"] = ((TextBox)item.FindControl("txtCutPaymentAmount")).Text; newRow["acutal_amount"] = ((Label)item.FindControl("lblAcutalAmount")).Text; newRow["bank_no"] = ((Label)item.FindControl("lblBankNo")).Text; newRow["bank_name"] = ((Label)item.FindControl("lblBankName")).Text; dt.Rows.Add(newRow); } rptTest.DataSource = dt; rptTest.DataBind(); }
因为Repeater是服务端控件,所以用它动态增加或删除一行会刷新页面。用户体验远没有用 knockoutjs 绑定的效果好
附: