3

I have an aspx page made with ASP.NET Web Forms. What i need to create is two asp:textbox fields. 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.

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?

How can i do this?

3
  • you can but you will need to maintain the state dynamically added control. Commented Jan 24, 2013 at 9:03
  • @krshekhar Ok, i guess that's fine? :) Sorry, i'm really new to ASP.NET, so i'm not really sure what to do with your advice :( Commented Jan 24, 2013 at 9:04
  • you can go through this link codeproject.com/Questions/435801/… Commented Jan 24, 2013 at 9:08

2 Answers 2

7

In your aspx file:

 <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:

 protected void btnAdd_Click(object sender, EventArgs e)
 {
      TextBox tb1 = new TextBox();
      TextBox tb2 = new TextBox();
      ph.Controls.Add(tb1);
      ph.Controls.Add(tb2);
 }
Sign up to request clarification or add additional context in comments.

2 Comments

This requires a postback... for dynamically adding new textboxes, javascript would be more user-friendly. Also you are missing an s on EventArg... And also this will not allow for infinite adding.
OP did not state that postback is not allowed. And if javascipt is turned off, what then? This solution has no dependency on javascript and requires minor modifications to achieve true infinite adding. In my opinion, this points the OP to the right direction and answers the question. :) Will update the code to add s Thanks @MikeSmithDev
0

You can add this fields and make them hidden(invisible). And you can show fields on button click (with javascript). Dynamically adding serverside controls is problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.