1
votes

I want to create a form field using asp.net which the user can append new textbox controls. Initially, there will be only one textbox and as the user will click on append textbox button new textbox controls will be generated and the previous textboxs will remain. Any suggestions? (apart an input which the user can give the number of textboxes and a submit button that will generate the provided number)

Edit: I'm working with asp.net and I use visualstudio

In order to ease the understanding I'll provide an example:

In the usercontrol

 <asp:TextBox Text="Item Name" ID="ItemName" runat="server"></asp:TextBox>
    <asp:TextBox Text="Item Desct" ID="Description1" runat="server"></asp:TextBox>
    <asp:Button OnClick="addDesc_click" ID="addDesc" runat="server" Text="Add More Descriptions" />

So basically, whenever the user clicks the button I want to create more textboxes in code behind. The new textboxes will be generated with a similar ID of the first TextBox e.g Description2, Description3 etc...

3
silverlight? wpf? asp? winforms? (need more information)Muad'Dib
This is a good question, you just need to tell us what you're working with—asp.net, winforms, etc.Adam Rackis

3 Answers

1
votes

The suggestion from Levisaxos works for ASP.NET, if you want to post back. If you want this client side, looking into JavaScript, or (easier) a JavaScript library like JQuery.

If this is some type of windows application, or JavaScript, you follow the same basic methodology, but you don't have to worry about client side scripting.

0
votes

Simple mode:

public void ButtonClick() 
{
    var bt = new TextBox();
    this.Controls.Add(bt);
}

if you want some more activity with it add all textbox's to a List and figure out a way to get their texts and such.

Also be sure to change each textbox's top or position property else they will just stack on top of eachother.

Hope this helps.

0
votes

I had to re ask the question with a more specific title and I got the response there. For the answer check this post. Persistent dynamic control in ASP.Net