Control Arrays in ASP.Net

  • Thread starter Thread starter Elmo Watson
  • Start date Start date
E

Elmo Watson

in VB6, I used to be able to create an array of controls:
textbox(0)
textbox(1)
textbox(2)....etc

That way, I could do a for/next loop and run through them with code, using
their index number as a key for the routine...

I tried to do this with panels, but adding the parentheses to the ID throws
an error message that it's not possible.....

How can you do this with ASP.Net (or is it possible at all)?
 
No you can't do this in asp.net like you are after, but you can get around it fairly easily.

For your problem in general, you can try this, name all your textboxes something like this

txtBox1
txtBox2
txtBox3

then in code, you can do this

for i = 1 to 3
dim txtBox as TextBox = ctype(FindControl("txtBox" & i.tostring), TextBox)
txtBox.text = whatever(i)
next

If you had controls you needed to handle events with, you can have mutiple events with the handles clause
Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click, anotherbtnFile.click

And if you really wanted to have fun, write the handler around the command event, and set commandname/commandarguments for each Button

Hope this helps,
--Michael
 
Every Server Control has a Collection called Controls, which is a Collection
of all child Controls inside it. You can certainly iterate through that
Collection. For example:

Dim i As Integer
For i = 0 To Form1.Controls.Count - 1
Form1.Controls(i)...
Next

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

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

Back
Top