javascript:
function CheckAll()
{
var checkboxlist1;
var checkboxlist2;
checkboxlist1=document.getElementById("CheckBoxList2");
checkboxlist2=document.getElementById("CheckBoxList1");
if(checkboxlist1.rows[0].cells(0).childNodes(0).checked)
{
for (i = 0; i < checkboxlist2.rows.length; i++)
{
checkboxlist2.rows[i].cells(0).childNodes(0).checked=true;
}
}
else
{
for (i = 0; i < checkboxlist2.rows.length; i++)
{
checkboxlist2.rows[i].cells(0).childNodes(0).checked=false;
}
}
}
function CheckAlone()
{
var checkboxlist1;
var checkboxlist2;
checkboxlist1=document.getElementById("CheckBoxList2");
checkboxlist2=document.getElementById("CheckBoxList1");
var count=0;
for (i = 0; i < checkboxlist2.rows.length; i++)
{
if( checkboxlist2.rows[i].cells(0).childNodes(0).checked==false)
{
count++;
}
}
if(count>0)
{
checkboxlist1.rows[0].cells(0).childNodes(0).checked=false;
}
}
.asp 文件
<table width="369px" style="background-color: antiquewhite" >
<tr>
<td style="text-align: center;" >
<asp:CheckBoxList id="CheckBoxList2" runat="server" Font-Names="宋体" Font-Size="10pt" ForeColor="Navy" Width="103px" OnPreRender="CheckBoxList2_PreRender" >
<asp:ListItem>全选</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
</table>
.cs文件
protected void CheckBoxList2_PreRender(object sender, EventArgs e)
{
CheckBoxList2.Attributes.Add("onclick", "CheckAll()");
CheckBoxList1.Attributes.Add("onclick", "CheckAlone()");
}