Reputation: 221
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?
Upvotes: 0
Views: 1828
Reputation: 6740
You can try by using FindControl
for(int i = 1; i <= 4; i++){
var label = ((Label)FindControl("lbl_Text" + i));
if(label != null){
label.Text = "hello";
}
}
Upvotes: 2