Creating Conrols dynamically

jamil

Newcomer
Joined
Aug 22, 2006
Messages
1
Hi Guys im new here. My question is on Dynamic controls. I have built my controls dynamically like this
Dim intCount as integer
For intCount = 1 to 10 then
dim MyTextBox as new TextBox
MyTextBox.Id="txtbox"
PlaceHolder.Controls.Add(MyTextBox)
Next
as you can see this code creates 10 textboxes. I need to use this textboxes to insert records into the database. Please help me out
 
As far as interacting with the database, that is not my area of expertise, but I do know that it is a good idea to track your textboxes some how. Your best bet in a case like this seems to be to use an array. In our simplest case, it would look something like this:
[Vb]
'Declare an array at class-level scope so we can use it in other functions
Dim myBoxes As TextBox()

'This code will be run when you want textboxes
Sub MakeMyBoxes()
Dim boxCount As Integer = 10
myBoxes = New TextBox(boxCount) {} 'Create an array

For i As Integer = 1 to boxCount
Dim MyTextBox as New TextBox
myBoxes(i) = MyTextBox

MyTextBox.Id="txtbox"
PlaceHolder.Controls.Add(MyTextBox)
Next
[/CODE]
Now that you have them stored in a variable in the form, when you want to store them in the database (say, for example, when the user clicks submit) you can loop through the textboxes in the array, get the .Text proprerty, and process it accordingly.

If you want a better handle on dynamic control creation you might want to read this tutorial that I wrote.
 
Back
Top