0

我想多次更改 asp 标签。

这是asp.net代码

<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="">

而不是使用这个:

C# 代码

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

我尝试使用 for 循环

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

我得到这个错误......

无法将 [] 索引应用于“system.web.ui.webcontrols.label”类型的表达式

有没有人可以帮助我?

4

1 回答 1

2

您可以尝试使用FindControl

for(int i = 1; i <= 4; i++){
  var label = ((Label)FindControl("lbl_Text" + i));
  if(label != null){
     label.Text = "hello";
  }
}
于 2013-11-08T03:50:31.800 回答