|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
For-Next Loop of Controls?
Hi all! I am new to this message board, so this is my first post.<br>Hopefully someone will be able to answer my quesion.<br><br>The basic question is, How would I create a For-Next loop to loop through multiple controls on a page to perform the same action to each control. Here is the example:<br><br>Lets say I have 20 Labels, Label1, Label2, Label3.....Label20<br>and each Label control has different text assigned to it.<br><br>Now a button is pushed, and each label will be assigned the same text = "New Text"<br><br>I could easily code:<br>Label1.Text = "New Text"<br>Label2.Text = "New Text"<br><br>all the way to:<br><br>Label20.Text = "New Text"<br><br>And this would work. But the purpose I need this for, it's a lot more complicated than just a simple text assignment. I am performing 5 of the same actions on 20 different controls. That's a lot of code that can be cleaned up in a loop. I am showing this as an example to help me understand the concept of looping through controls.<br><br>I want to make a For-Next loop to loop through all 20 controls, and add the "New Text" to the Text property of each Label.<br><br>I tried the following:<br><br> Dim x As Integer<br> Dim olabel As Object<br><br> For x = 1 To 20<br> olabel = ("Label" & x) <br> olabel.Text = "New Text" <br> x = x + 1<br> Next<br><br>It does not work. What this does, is put a STRING of "Label1" into the olabel object.<br>There is no .Text property of a STRING, and therefore the above is invalid code.<br><br>Does anyone know how I could resolve this into a working loop through all the controls and perform the same task to each control? Help is appreciated! Thank you! <img border="0" src="/forum/emoticons/smile.gif" height="15" width="15" alt="smile" />
|
|
#2
|
|||
|
|||
|
I figured out how to resolve my problem.<br><br>For those who are interested, and would like to know how to loop through controls and use their properties, here is how I did it:<br><br> Dim x As Integer = 0<br> Dim arrayUserLabels(50) As Label 'Declare 50 Labels in the Array List<br> arrayUserLabels(0) = Me.lblUser0 <br> arrayUserLabels(1) = Me.lblUser1<br> arrayUserLabels(2) = Me.lblUser2<br> arrayUserLabels(3) = Me.lblUser3<br> ........<br> arrayUserLabels(49) = Me.lblUser49<br><br> For x = 0 To 49<br> arrayUserLabels(x).Text = "New Text" <br> x = x + 1<br> Next<br><br>Though the work of adding the labels to the array is needed here, I only have to do it once.<br>Whenever I need to do something to all those controls, all I need to do is loop through that arraylist.<br><br>If anyone knows of a better way, so that all the work could be done in the loop instead of creating an array, I would appreciate if you posted here. Thanks!
|
|
#3
|
|||
|
|||
|
The Page contains a Controls collection, that you can add, literals, textboxes etc to:<br>Try wrapping this in a server side Script tag <br>Sub Page_Load<br> dim i as integer<br> for i = 1 to 30<br> Controls.Add(New LiteralControl("Hello"))<br> next<br>End Sub
|
|
#4
|
|||
|
|||
|
Thanks for the post, but I don't see how I can use that for the purpose that I need.<br><br>In trying to duplicate my example:<br><br>dim i as integer<br>for i = 1 to 30<br>Controls.Add(New Label(??))<br>next<br><br>What goes in the parenthesis? And I don't want to add controls to the page.<br>I want to use existing controls I have on the page, and conduct the same<br>method to all the controls.<br><br>From what I found, using the following works very well actually:<br> Dim x As Integer<br> Dim arrayUserLabels(4) As Label<br> arrayUserLabels(0) = Me.lblUser0 <br> arrayUserLabels(1) = Me.lblUser1<br> arrayUserLabels(2) = Me.lblUser2<br> arrayUserLabels(3) = Me.lblUser3<br><br> For x = 0 To 4<br> arrayUserLabels(x).Text = "New Text" <br> Next
|
|
#5
|
|||
|
|||
|
Try searching for Controls in the online help.<br>Then look at the section Adding Controls to Web Form Page Programatically.
|
![]() |
| Viewing: ASP Free Forums > Programming > .NET Development > For-Next Loop of Controls? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|