如何在DATAGRID上的第一列加上CHECKBOX
类别: ASP教程
全选/反选:
<input type="checkbox" name="selAll" onclick="SelectAll(this,\'DataGrid1\',0);">全选/反选
注SelectAll???嫡f明:
DataGrid1:?槟ataGrid的ID
0:?殂heckbox在Datagrid中的列位置:?陌檫始,不锢示出?淼牧胁凰慵br>
function SelectAll(cur,obj,item)
{
var boolSel = false;
if(cur.checked) boolSel=true;
var objTBChildren = document.all(obj).children;
for(var i=1; i<objTBChildren.length; i++)
{
if(objTBChildren[i].tagName=="TBODY")
{
var objTRS = objTBChildren[i].children;
for(var i=0; i<objTRS.length; i++)
{
try{
var objTDS = objTRS[i].children;
var chkCtrls = objTDS[item].children;
chkCtrls[0].checked = boolSel;
}catch(e){}
}
}
}
}
private void dgMission_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemIndex > -1)
{
((CheckBox)e.Item.Cells[0].FindControl("chkName")).Attributes.Add("onclick","changecolor(this)");
}
}
<script language="javascript">
function changecolor(sender)
{
var temp=sender.parentElement;
while(temp.tagName != \'TR\')
{
temp=temp.parentElement;
}
if(temp!=null)
{
temp.style.backgroundColor=\'Green\';
}
}
</script>
<input type="checkbox" name="selAll" onclick="SelectAll(this,\'DataGrid1\',0);">全选/反选
注SelectAll???嫡f明:
DataGrid1:?槟ataGrid的ID
0:?殂heckbox在Datagrid中的列位置:?陌檫始,不锢示出?淼牧胁凰慵br>
function SelectAll(cur,obj,item)
{
var boolSel = false;
if(cur.checked) boolSel=true;
var objTBChildren = document.all(obj).children;
for(var i=1; i<objTBChildren.length; i++)
{
if(objTBChildren[i].tagName=="TBODY")
{
var objTRS = objTBChildren[i].children;
for(var i=0; i<objTRS.length; i++)
{
try{
var objTDS = objTRS[i].children;
var chkCtrls = objTDS[item].children;
chkCtrls[0].checked = boolSel;
}catch(e){}
}
}
}
}
private void dgMission_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemIndex > -1)
{
((CheckBox)e.Item.Cells[0].FindControl("chkName")).Attributes.Add("onclick","changecolor(this)");
}
}
<script language="javascript">
function changecolor(sender)
{
var temp=sender.parentElement;
while(temp.tagName != \'TR\')
{
temp=temp.parentElement;
}
if(temp!=null)
{
temp.style.backgroundColor=\'Green\';
}
}
</script>
-= 资 源 教 程 =-
文 章 搜 索