how loop through various labels' text values

T

tony collier

hi

i have various labels on my page with ID's label_1, label_2 , label_3 etc.

How can i write a loop that sequentially changes their text values to
values i have in an array eg;.

for (i=0;i<max;i++){
label_1.text = labelarray
}

then on next 2 loops:


label_2.text=labelarray

and

label_3.text=labelarray



etc.etc.
 
G

Gerben van Loon

Hi Tony,

You could put the labels in an arraylist and then use the foreach statement
to go through this list. This will look something like this:

ArrayList labellist = new ArrayList()
labellist.add(label_1)
labellist.add(label_2)
labellist.add(label_3)

//Loop throught the list of labels
foreach(Label lbl in labellist)
{
lbl.text = labelarray
}

Or if this affects ALL labels which are on your page you could loop trough
the Page.Controls property

foreach(Label lbl in Page.Controls)
{
lbl.text = labelarray
}

Hope it helps. Cheers,

Gerben.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top