Search code examples
c#asp.netlabelfindcontrol

How to find label controls in an asp.net page


I have some label controls in a panel with id ResultsPanel. To find the label controls on the page, I did the following:

for (int ctr = 0; ctr < lblString.Length - 1; ctr++)
{
    //string labelID = string.Format("lblResult{0}", ctr);
    int mylbl = ctr + 1;
    string lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString())).Text;
    lblResult = lblString[mylbl].ToString();
}
lblResult1.Text = lblString.ToString();

lblString is a stringBuilder Object with 24 numbers. The Idea is to map each of the numbers in the stringbuilder object to labels in this manner:

lblResult1.Text = lblString[mylbl].ToString(); to the 24th label. But I can't seem to generate the labels and map the values to the label control.


Solution

  • Change the line to

    Label lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString()));
            lblResult.Text = lblString[mylbl].ToString();