c# append control on each click
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...
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.
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.
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
精彩评论