how loop through various labels' text values

  • Thread starter Thread starter tony collier
  • Start date Start date
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.
 
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.
 
Back
Top