日期:2014-05-17 浏览次数:20526 次
int i = 0;
int index = 3; //需要选中的checkbox
foreach(Control ctl in groupBox1.Controls)
{
if(ctl is CheckBox)
{
i++;
if(i == index)
{
(ctl as CheckBox).Checked = true;
break;
}
}
}
------解决方案--------------------
http://blog.sina.com.cn/s/blog_4cef5c7b0100ina6.html
------解决方案--------------------
生成时,CheckBox的tag中写入和name对应的值,这样遍历时,可以根据name的值和tag值对比找到 你要的CheckBox
如果web页,那就设置id或其他属性与name值对应,js遍历
------解决方案--------------------
比如
private void SetCheck(string name)
{
foreach (Control c in groupBox1.Controls)
{
if (c is CheckBox)
{
if (c.Tag.ToString() == name)
{
((CheckBox)c).Checked = true;
break;
}
}
}
}
------解决方案--------------------
我上面只是简单的写了个例子,你要用id匹配,可以这么写:
CheckBox chk = ctl as CheckBox;
if(chk.ID == yourID)
{
//...
}