Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...