Mohammad Khandordi
Mohammad Khandordi

Reputation: 65

how to change some labels in ASP.Net page by for loop form code behind?

I have 10 Label control in an ASP.Net page. their Id are in row like

label1, label2, label3, ... ,label10
I want to change their Text property to something like

Home1 , Home2, Home3, ... Home10
Can I do this from code behind by using For loop or something like that?

Upvotes: 0

Views: 854

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39966

Suppose your labels are inside a div tag (don't forget to add runat="server"):

<div id="labels" runat="server">
  <%--Your Labels--%>
</div>

And in the code behind:

int i = 1;

foreach (var item in labels.Controls)
{
    if (item is Label)
    {
        ((Label)item).Text = "Home" + i;
        i++;
    }
}

Upvotes: 1

Related Questions