62,050
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
CreateTextboxes(false);
}
private void CreateTextboxes(bool loadcompleted)
{
this.pnlTest.Controls.Clear();
int num = 上一次输出html时的动态文本框数量;
for (int i = 0; i < num; i++)
{
TextBox txtAdd = new TextBox();
txtAdd.ID = "txtIMEI" + (i + 1);
this.pnlTest.Controls.Add(txtAdd);
if (loadcompleted)
txtAdd.Text = "IMEI" + (i + 1);
}
}
private int 上一次输出html时的动态文本框数量
{
get
{
object x = ViewState["上一次输出html时的动态文本框数量"];
if (x == null)
{
x = 0;
ViewState["上一次输出html时的动态文本框数量"] = x;
}
return (int)x;
}
set
{
ViewState["上一次输出html时的动态文本框数量"] = value;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string result = string.Empty;
foreach (Control ctrl in this.pnlTest.Controls)
result += (result == string.Empty ? ", " : "") + ((TextBox)ctrl).Text + ",";
this.TextBox1.Text = result;
}
protected void Button2_Click(object sender, EventArgs e)
{
上一次输出html时的动态文本框数量 = int.Parse(this.TextBox2.Text.Trim());
CreateTextboxes(true);
}
</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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="pnlTest" runat="server">
</asp:Panel>
输入你要创建的TextBox数量:
<asp:TextBox ID="TextBox2" runat="server" Width="20px" Text="2"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="生成TextBox" /><br />
<asp:TextBox ID="TextBox1" runat="server" Width="698px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="获取值" />
<br />
<br />
<asp:Button ID="abc" runat="server" Text="最基本测试" /><br />
</form>
</body>
</html>