假设你是WINForm,并且form1上有3个Labelfor (int i = 1; i <= 3; i++)
{
Control[] controls = this.Controls.Find("label" + i, true);
foreach (Control c in controls)
{
if (c.GetType() == typeof(Label))
{
((Label)c).Text = "某一值";
}
}
}
{
Control[] controls = this.Controls.Find("label" + i, true);
foreach (Control c in controls)
{
if (c.GetType() == typeof(Label))
{
((Label)c).Text = "某一值";
}
}
}
照你的方法出现上述错误?
{
Label lbl = (Label)this.FindControl("label"+i);
lbl.Text = "想赋的值";
}
for (int i = 1; i <= 3; i++)
{
((Label)Page.FindControl("Label" + i)).Text = "xxx";
}
foreach (Control c in this.Page.Form .Controls )
{
if (c is Label )
{
((Label)c).Text = "hello";
}
}这个是挨个遍历赋的值相同
呵呵.这个遍历会把所有LABEL都赋值,如果有其它名称而又不想改它的值的时候,这样就会小有问题.