js控制Gridview中的checkbox全选
js代码部分
以下两种都可以
------------------------------------------------------------------------------------------------------------
function CheckAll(oCheckbox) {
var gvList = document.getElementById("<%=gridView.ClientID %>");
for (i = 1; i < gvList.rows.length; i++) {
gvList.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked;
}
}
------------------------------------------------------------------------------------------------------------
function SelectAll(chkbox)
{
var theBox=chkbox;
xState=theBox.checked;
elem=theBox.form.elements;
for(i=0;i<elem.length;i++)
if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
{
if(elem[i].checked!=xState)
elem[i].click();
}
}
.net代码部分
<asp:TemplateField>
<HeaderTemplate>
<input id="chkHeader" onclick="SelectAll(this)" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<input id="chkItem" type="checkbox" name="chkItem" runat="server" value='<%#Eval("UserID") %>' />
</ItemTemplate>
</asp:TemplateField>
获取选中的checkbox的值
string str = "";
for (int i = 0; i < this.GridViewUser.Rows.Count; i++)
{
HtmlInputCheckBox chk = (HtmlInputCheckBox)this.GridViewUser.Rows[i].FindControl("chkItem");
if (chk.Checked == true)
{
str += chk.Value+",";
}
}
-----------------------------------------------------------------------------------------------------
function changStatus(obj) { // alert($("#allcheckbox").is(":checked")); $('input[class="checkboxList"]').attr("checked", $("#allcheckbox").is(":checked"));
}