我有3个控件,叫lable1、lable2、lable3
我想通过循环实现应该怎么写?for(int i=1;i<=3;i++)
{
lable[i].Text="..";
}
这样不行
请问lable后面的序号应该怎么写?

解决方案 »

  1.   

    foreach(WebControl webControl in this.Controls)
    {
    if(webControl is Label && webControl.ID == "label1")
    {

    }
    }
      

  2.   

    for(int i=1;i<=3;i++)
    {
        string name =  lable + "i" ;
        foreach(WebControl webControl in this.Controls)
        {
            if(webControl is Label && webControl.ID == name)
            {
                  webControl.Text = name;
            }
       }
    }
      

  3.   

    或者用数组
    如:
    Label[] lbl=new Label[3];
    for(int i=0;i<3;i++)
    {
        lbl[i]
    }
      

  4.   

    for(int i=1;i<=3;i++)
    {
       string lcID="lable"+i;
       Control loCtrl=this.FindControl(lcID);
       if (loCtrl!=null&&loCtrl is Label)
       {
         ((Label)loCtrl).Text="sdfa";
       }
    }
     
      

  5.   

    zahota的方法 还是要借助于FindControl的,否则手工把Label赋给数组还是没有减轻工作量。
    个人以为  syfsz 的方法应该是效率最好的