首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DataGrid的一个格中放了多个CheckBox,怎么遍历某一格中的所有CheckBox

2012-01-10 
DataGrid的一个格中放了多个CheckBox,如何遍历某一格中的所有CheckBoxWebForm里DataGrid的一个列中包含了

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);
// 更多操作
}
}

热点排行