C# How can loop through a series of labels to update the labels text

I am getting some great help from some great C# minds! Here is another question.

I am cycling through a loop six times to update six labesl (i.e. label1, label2, label3.. etc) rather than label1.Text and label2.Text etc I want to loop through them so I tried:

 label + Convert.ToString(intCounter).Text = newLabel;

It really doesn't like that. Any thoughts?

Karl66
Karl66Asked:
Who is Participating?
 
gocemiCommented:
Try this:
for(int i=0; i < x; i++)
{
Control c = this.FindControl("label "+i);
if(c!= null and c is TextBox )
(c as TextBox).Text = [Value];
}
0
 
Bob LearnedCommented:
Missing information:
1) .NET framework version (2.0, 1.1, ...)
2) Application type (WinForms, ASP.NET, ...)

Bob
0
 
fmonroyCommented:
A quick and dirty way is:

foreach(Control Ctrl in Controls) {
      if(Ctrl.Name == "label1")
            ((Label)Ctrl).Text = "NEWTEXT";
}

just add ORs in the if or any logic you need.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.