referencing a textbox created on the fly?

  • Thread starter Mad Scientist Jr
  • Start date
M

Mad Scientist Jr

i'm working on a page that creates a HTML table with a variable number
of textboxes on the fly, like so:

cell = New HtmlTableCell
cell.VAlign = "Top"
TextBoxNext = New TextBox
TextBoxNext.ID = "TextBox" & iLoopRows.ToString
TextBoxNext.Text = ""
cell.Controls.Add(TextBoxNext)
row.Cells.Add(cell) ' Add the cell to the row

my question is, on postback, how do you dynamically reference the
textboxes that were created? my page simply names the controls
"TextBox1", "TextBox2" etc. and puts the number of textboxes in a
hidden field txtExistingControls. Then I'd like to be able to look
through them, like so:

If txtExistingControls.Value > 0 Then
For iLoopRows = 1 To txtExistingControls.Value
txtOut.Text = "Value of " & iLoopRows.ToString & "=" & TextBox
<- how to reference the control?
Next iLoopRows
Else
txtOut.Text = "no controls available"
End If

is there a way to dynamically reference the created controls (as
opposed to by name Textbox1.text etc), without having to resort to
Request.Forms ?

thanks in advance
 
S

Stanley

An easy way to do this would be to declare your textboxes at the class
level. The when you do TextBoxNext = New TexBox you are still creating your
new textbox. Then anywhere in your app you can reference it.
 
M

Mad Scientist Jr

Thanks for your answer, but the problem is that we're not sure how
many textboxes will be created. It could be any number. So declaring
them ahead of time won't work (unless it can be done as a control
array, which I think .NET has done away with?)

I am able to create them on the fly, and as long as I know how many
there are (tracked via hidden textbox for example), read them in with
request.form. However these don't persist like normal server controls
do, they have to be re-populated. The textboxes would have to 1.
persist and 2. be referencable in some variable way, aka
TextBox1(iIndex).text
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top