日期:2014-05-18  浏览次数:20533 次

DataGrid的一个格中放了多个CheckBox,如何遍历某一格中的所有CheckBox
WebForm里DataGrid的一个列中包含了很多的CheckBox,我需要遍历出某一行中此列中的所有CheckBox,该如何遍历出呢?下面是我的代码,没有遍历出CheckBox,请各位指教
string   role   =   null;
foreach(object   obj   in   Form1.Controls)
{
if(obj   is   DataGrid)
{
foreach(object   nobj   in   this.DataGrid1.Controls)
{
if(nobj   is   CheckBox)
{
CheckBox   cb   =   (CheckBox)nobj;
if(cb.Checked   ==   true)
{
role   +=   "1 ";
}
else
{
role   +=   "0 ";
}
}
}
}
}


------解决方案--------------------
foreach(DataGridItem item in this.DataGrid1.Items)
{
foreach(object nobj in items.Controls)
{
if(nobj is CheckBox)
{
CheckBox cb = (CheckBox)nobj;
if(cb.Checked == true)
{
role += "1 ";
}
else
{
role += "0 ";
}
}
}
}
------解决方案--------------------
<script language= "javascript " type= "text/javascript ">
function selectAll(obj)
{
var theTable = obj.parentElement.parentElement.parentElement;
var i;
var j = obj.parentElement.cellIndex;

for(i=0;i <theTable.rows.length;i++)
{
var objCheckBox = theTable.rows[i].cells[j].firstChild;
if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
}
}
</script>
------解决方案--------------------
1楼方法正解,2楼怎么用js了?
------解决方案--------------------
for (int i=0; i <dgSympList.Items.Count; i++)
{
CheckBox chbItem = ((CheckBox)dgSympList.Items[i].Cells[0].FindControl( "chbSelect "));
if (chbItem.Checked)
{
// 操作...
}
}
------解决方案--------------------
for(int i=0;i <this.DataGrid1.rows;i++)
{
CheckBox cb = (CheckBox)DataGrid1.rows[i].cells[num].controls.findcontrol( "controlid ");
if(cb!=null)
{
}
}
------解决方案--------------------
1。
天啊,我的表述能力是如此之差!

2。
protected MyDataGrid_ItemComand(object sender, DataGridItemComandEventArgs e){
int cellIndex = 1; // 第二列,索引为 1
// 遍历子控件
foreach(Control ctrl in e.Item.Cells[cellIndex].Controls) {
if(ctrl is CheckBox) { // 确认是否为 CheckBox
CheckBox chk = (CheckBox)ctrl; // 显示转换
Response.Write(chk.Checked);
// 更多操作
}
}