我从数据库中取出字符串需要将字符对应的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;
}有什么更好的方法吗?
已开始是这样写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;
}有什么更好的方法吗?
foreach(DataRow mRow in YouSeachTable.Rows)
{
CheckBox ctlchk =Page.FindControl(mRow["ControlName"]) as CheckBox;
if(ctlchk!=null)
ctlchk.Checked = True;
}
Page.FindControl没有找到引用
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;
}
{
string S = "checkBox1";
Control[] vControls = Controls.Find(S, true);
if (vControls.Length > 0) ((CheckBox)vControls[0]).Checked = true;
}
string S = "checkBox1";
foreach (Control vControl in Controls)
{
if (vControl.Name == S)
{
((CheckBox)vControl).Checked = true;
break;
}
}
for (int i=0;i<tabPage1.Controls.Count;i++)
{
if (tabPage1.Controls[i].Name == s)
{
((CheckBox)tabPage1.Controls[i]).Checked=true;
break;
}
}
if(cb.Name=="checkBox"+s){//s就是"1","2","3".....
cb.Checked=true;
break;//找到就跳出循环
}
}