程序代码
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Diagnostics" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.DataBind();
}
}
public override void DataBind()
{
this.fvInput.DataSource = new int[this.fvInput.Items.Count];
base.DataBind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
List<string> list = this.GetValues(this.fvInput);
foreach (string val in list)
{
sb.AppendFormat("{0}<br/>", val);
}
this.litOutput.Text = sb.ToString();
}
protected List<string> GetValues(Repeater rep)
{
List<string> list = new List<string>();
foreach (RepeaterItem item in this.fvInput.Items)
{
TextBox tb = item.FindControl("tbName") as TextBox;
Debug.Assert(tb != null);
list.Add(tb.Text);
}
return list;
}
protected void btnAddItem_Click(object sender, EventArgs e)
{
List<string> list = GetValues(this.fvInput);
list.Add("default");
this.fvInput.DataSource = list;
this.fvInput.DataBind();
}
</script>
<!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">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Repeater runat="server" ID="fvInput" >
<ItemTemplate>
<asp:TextBox runat="server" ID="tbName" Text='<%#Container.DataItem%>'/><br />
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" ID="btnAddItem" Text="+" OnClick="btnAddItem_Click"/>
<asp:Button runat="server" ID="btnSubmit" Text="OK" OnClick="btnSubmit_Click"/><br />
<asp:Literal runat="server" ID="litOutput" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Diagnostics" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.DataBind();
}
}
public override void DataBind()
{
this.fvInput.DataSource = new int[this.fvInput.Items.Count];
base.DataBind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
List<string> list = this.GetValues(this.fvInput);
foreach (string val in list)
{
sb.AppendFormat("{0}<br/>", val);
}
this.litOutput.Text = sb.ToString();
}
protected List<string> GetValues(Repeater rep)
{
List<string> list = new List<string>();
foreach (RepeaterItem item in this.fvInput.Items)
{
TextBox tb = item.FindControl("tbName") as TextBox;
Debug.Assert(tb != null);
list.Add(tb.Text);
}
return list;
}
protected void btnAddItem_Click(object sender, EventArgs e)
{
List<string> list = GetValues(this.fvInput);
list.Add("default");
this.fvInput.DataSource = list;
this.fvInput.DataBind();
}
</script>
<!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">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Repeater runat="server" ID="fvInput" >
<ItemTemplate>
<asp:TextBox runat="server" ID="tbName" Text='<%#Container.DataItem%>'/><br />
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" ID="btnAddItem" Text="+" OnClick="btnAddItem_Click"/>
<asp:Button runat="server" ID="btnSubmit" Text="OK" OnClick="btnSubmit_Click"/><br />
<asp:Literal runat="server" ID="litOutput" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>