简体   繁体   English

在ASP.NET中动态添加asp:textbox

[英]Add asp:textbox dynamically in ASP.NET

I have an aspx page made with ASP.NET Web Forms. 我有一个用ASP.NET Web表单制作的aspx页面。 What i need to create is two asp:textbox fields. 我需要创建的是两个asp:textbox字段。 I want to be able to dynamically add two new fields below that with the press of a button. 我希望能够通过按下按钮在其下动态添加两个新字段。

So basically i want to be able to add an infinite amount of "new" textfields. 因此,基本上我希望能够添加无限数量的“新”文本字段。 But i'm not sure how to do this in ASP.NET. 但是我不确定如何在ASP.NET中执行此操作。

Is there perhaps a way to create an arrat of those textfields? 也许有办法创建这些文本字段的集合吗? So that when a form is posted i can easily iterate over them? 这样,当发布form时,我可以轻松地遍历它们吗?

How can i do this? 我怎样才能做到这一点?

In your aspx file: 在您的aspx文件中:

 <asp:TextBox runat="server" ID="textbox1"/>
 <asp:TextBox runat="server" ID="textbox2"/>

 <asp:Button runat="server" ID="btnAdd" OnClick="btnAdd_Click" />
 <asp:PlaceHolder runat="server" ID="ph" />

In your aspx.cs file: 在您的aspx.cs文件中:

 protected void btnAdd_Click(object sender, EventArgs e)
 {
      TextBox tb1 = new TextBox();
      TextBox tb2 = new TextBox();
      ph.Controls.Add(tb1);
      ph.Controls.Add(tb2);
 }

You can add this fields and make them hidden(invisible). 您可以添加此字段并使它们隐藏(不可见)。 And you can show fields on button click (with javascript). 您可以在单击按钮时显示字段(使用javascript)。 Dynamically adding serverside controls is problem. 动态添加服务器端控件是有问题的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM