JS脚本:
function SetCheckBoxState() { var dom=document.all; var el=event.srcElement; if(el.tagName=="INPUT" && el.type.toLowerCase()=="checkbox") { for(i=0;i<dom.length;i++) { if(dom[i].tagName=="INPUT" && dom[i].type.toLowerCase()=="checkbox") { dom[i].checked=false; } } } el.checked=!el.checked; }
前台代码:
<asp:DataList ID="dlRoleList" runat="server" RepeatColumns="3" Width="100%" DataKeyField="RoleID" OnItemDataBound="dlRoleList_ItemDataBound"> <ItemTemplate> <asp:CheckBox ID="cbRoles" Text='<%# DataBinder.Eval(Container.DataItem,"RoleName") %>' runat="server" /> </ItemTemplate> </asp:DataList>
后台代码实现:
protected void dlRoleList_ItemDataBound(object sender, DataListItemEventArgs e) { if(e.Item.ItemIndex<0) return; if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { System.Web.UI.WebControls.CheckBox chkExport; chkExport = (CheckBox)e.Item.FindControl("cbRoles"); chkExport.Attributes.Add("OnClick","SetCheckBoxState()"); } }