How does one dynamically generate a TextBox control at run-time as a result of a button click? For each button click, I would like to create a TextBox control along with corresponding dynamic labels. I would like to do this within ASP.NET using the C# language.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
To add multiple controls as you asked, use a for loop:
But the controls (textboxes) overlaps. You need to organize their locations.
EDIT: E.g
Well I would suggest you to first create a grid and to specify the wanted number of rows and columns, in order to have everything organized.
Your MainWindow.xaml should look like this:
Next we are trying to build two rows of textboxes which will be dynamically created on runtime. By using two different buttons, one for the first row textboxes and the other for the second row, we will be able to spawn the desired textboxes.
I hope it is clear enough. You just have to add one button for each of the two methods listed above. Just like this:
The following will create the controls:
you can then set the properties etc that you want.
Then find somewhere on your page to add them to, say you have a panel called panel1, then do the following:
However, doing this will not work after postback - you need to recreate the dynamic controls yourself on a postback.
Say you have the following page:
When you do a postback, only the controls that are defined in the above page will be generated for you. Controls you have dynamically added need to be re-created by you (for example in the Page_Load).
To do this, the simplest way is to remember the total number of controls you have added in the viewstate, then add that many controls back in when a postback occurs.
The following should get you started:
The code below shows how to print a label and text box according to the value selected in the dropdownlist. The two placeholders are used so that they can be appropriately placed in two different table divisions