How to change the text of the multiple asp:label using for loop in C# ASP.NET

Posted by Minelava on Stack Overflow See other posts from Stack Overflow or by Minelava
Published on 2013-11-08T03:44:21Z Indexed on 2013/11/08 3:53 UTC
Read the original article Hit count: 169

I want to change the asp label multiple times.

Here is the asp.net code

<asp:Label ID="lbl_Text1" runat="server" Text="">
<asp:Label ID="lbl_Text2" runat="server" Text="">
<asp:Label ID="lbl_Text3" runat="server" Text="">
<asp:Label ID="lbl_Text4" runat="server" Text="">

Instead of using this:

C# Code

lbl_Text1.Text = "hello";
lbl_Text2.Text = "hello";
lbl_Text3.Text = "hello";
lbl_Text4.Text = "hello";

I tried to use for loop

for (int i = 1; i <= 4; i++)
{
    lbl_Text[i].Text = "hello";
} 

And I get this error.....

cannot apply indexing with [] to an expression of type 'system.web.ui.webcontrols.label'

Is there anyone can help me on that?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET