动态添加多个文本框问题

kadsyt 2007-11-22 03:47:00
我想实现以下功能:每按一次按钮就自动生成两个或多个文本框。
一次生成一个的代码已经找到了,不知道怎么改,c#的,各位高手帮帮忙:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
for (int i = 0; i < TextBoxsList.Count;i++)
{
string txtID = TextBoxsList[i].ToString();
TextBox tmp = new TextBox();
tmp.ID = txtID;
Page.Form.Controls.Add(tmp);
tmp.Text = Request.Form[txtID].ToString();

}
}
}
private ArrayList TextBoxsList
{
get
{
if (ViewState["myTextBoxsList"] == null)
return new ArrayList();
else
return ViewState["myTextBoxsList"] as ArrayList;
}
set
{
ViewState["myTextBoxsList"] = value;
}
}


protected void Button1_Click(object sender, EventArgs e)
{

TextBox tmp = new TextBox();
tmp.ID = "TextBox" + TextBoxsList.Count.ToString();
Page.Form.Controls.Add(tmp);

ArrayList array = TextBoxsList;
array.Add(tmp.ID);
TextBoxsList = array;
}

}
...全文
116 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
myyihua 2007-11-22
  • 打赏
  • 举报
回复
当然你也可以指定Number的个数,这样的话就每次按你的按钮可以创建出你指定个数的textbox了
myyihua 2007-11-22
  • 打赏
  • 举报
回复
首先你肯定要传入一个数据,需要创建的个数;

protected void Button1_Click(object sender, EventArgs e)
{
int number = 你需要创建的个数
for( int i= 0;i<number;i++)
{
TextBox tmp = new TextBox();
tmp.ID = "TextBox " + TextBoxsList.Count.ToString();
Page.Form.Controls.Add(tmp);
ArrayList array = TextBoxsList;
array.Add(tmp.ID);
TextBoxsList = array;
}

}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧