JS:
//判定是单选还是全选
var isMulti = "<% =Request.QueryString["mode"]%>"; //全选 function selectAll(ctlName, bool) { var ctl = document.getElementById(ctlName); var checkbox = ctl.getElementsByTagName('input'); for (var i = 0; i < checkbox.length; i++) { if (checkbox[i].type == 'checkbox') { checkbox[i].checked = bool; } } } var ClickNum=0;//判断同一checkbox连续点击次数 var PreCheckboxID="";//记录点击checkbox的ID //listId为控制checkbox的范围 //SelectRadio是点击checkbox自己 function SelectRadio(listId,SelectRadio) { if(isMulti == "") { ///判断是否连击同一个checkbox if(PreCheckboxID==SelectRadio.id) { ClickNumClickNum = ClickNum + 1; } else { PreCheckboxID = SelectRadio.id; ClickNum = 0; } //找到控制范围 var GridViewtableSearchList = document.getElementById(listId); //找到控制范围下所有input var objs = GridViewtableSearchList.getElementsByTagName("input"); //找到控制范围下所有checkbox并都变为false for(var i = 0; i < objs.length; i++) { if(objs[i].type.toLowerCase() == "checkbox" ) objs[i].checked = false; } var SelectRadioSelectRadioID = SelectRadio.id; //如果连击次数为奇次checked为true偶次为false if(ClickNum%2!=1) { document.getElementById(SelectRadioSelectRadioID).checked = true; } else { document.getElementById(SelectRadioSelectRadioID).checked = false; } } }
html
<asp:GridView ID="MainList" runat="server" AutoGenerateColumns="False" CssClass="DIVGrid" DataKeyNames="UG_ID" CellPadding="2" EmptyDataText="没有记录!" Width="520px" OnPageIndexChanging="MainList_PageIndexChanging"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox runat="server" ID="cbHead" onclick="javascript:selectAll('MainList',this.checked);" Text="选择"></asp:CheckBox> </HeaderTemplate> <ItemTemplate> <asp:CheckBox runat="server" ID="cbItem" onclick="javascript:SelectRadio('MainList',this);"> </asp:CheckBox> </ItemTemplate> <HeaderStyle HorizontalAlign="Center" Width="60px" /> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:BoundField DataField="UG_Name" HeaderText="部门"> <HeaderStyle HorizontalAlign="Center" Width="60" /> </asp:BoundField> </Columns> </asp:GridView>