我从数据库中取出字符串需要将字符对应的CheckBox选中CheckBox有20多个string值就是CheckBox的text,实在不行就是CheckBox的Name
已开始是这样写switch(s)       
{         
case "1":
checkBox1.Checked=true; 
break;
case "2":            
    checkBox2.Checked=true; 
break;
case "3":            
checkBox3.Checked=true; 
break;
case "4":            
checkBox4.Checked=true; 
 break;
case "5":            
checkBox5.Checked=true; 
break;
}有什么更好的方法吗?

解决方案 »

  1.   


    foreach(DataRow mRow in YouSeachTable.Rows)
    {
    CheckBox ctlchk =Page.FindControl(mRow["ControlName"]) as CheckBox;
    if(ctlchk!=null)
       ctlchk.Checked = True;
    }
      

  2.   

    CheckBox ctlchk =Page.FindControl(mRow["ControlName"]) as CheckBox;
    Page.FindControl没有找到引用
      

  3.   

    还是不包含是要加什么using吗?
      

  4.   


                CheckBox dd = myControl[0] as CheckBox;foreach(DataRow mRow in YouSeachTable.Rows)
    {
    Control[] myControl = this.Controls.Find(mRow["ControlName"], true) as Control[]; 
    CheckBox ctlchk = myControl[0] as CheckBox;
    if(ctlchk!=null)
       ctlchk.Checked = True;
    }
      

  5.   

    private void button1_Click(object sender, EventArgs e)
    {
        string S = "checkBox1";
        Control[] vControls = Controls.Find(S, true);
        if (vControls.Length > 0) ((CheckBox)vControls[0]).Checked = true;
    }
      

  6.   

    //在原始一点,这样也成
    string S = "checkBox1";
    foreach (Control vControl in Controls)
    {
        if (vControl.Name == S)
        {
            ((CheckBox)vControl).Checked = true;
            break;
        }
    }
      

  7.   

    现在采取for循环处理,但明明知道name还要这样真是。
    for (int i=0;i<tabPage1.Controls.Count;i++)
    {
    if (tabPage1.Controls[i].Name == s)
    {
    ((CheckBox)tabPage1.Controls[i]).Checked=true; 
    break;
    }
    }
      

  8.   

    Page.RegisterStartUp("","<script>document.all.item("CheckBox"+s).checked=true;</script>");
      

  9.   

    把20个checkbox都放在一个panel里~~~foreach(CheckBox cb in Panel1.Controls){
        if(cb.Name=="checkBox"+s){//s就是"1","2","3".....
            cb.Checked=true;
            break;//找到就跳出循环
        }
    }