.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 17th, 2003, 09:40 PM
Stallion112 Stallion112 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 29 Stallion112 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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" />

Reply With Quote
  #2  
Old May 18th, 2003, 12:52 AM
Stallion112 Stallion112 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 29 Stallion112 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #3  
Old May 18th, 2003, 01:31 AM
JGF JGF is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 3 JGF User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old May 18th, 2003, 05:28 AM
Stallion112 Stallion112 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 29 Stallion112 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old May 18th, 2003, 02:19 PM
JGF JGF is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 3 JGF User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Try searching for Controls in the online help.<br>Then look at the section Adding Controls to Web Form Page Programatically.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgramming.NET Development > For-Next Loop of Controls?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT