CheckBox为CheckBoxList实现全选或全取消选择
某一个时候,CheckBoxList的选择太多,用户需要一个全选或全取消的功能。下面Insus.NET使用Javascript来实现它。
准备好一个对象:

using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for MusicType /// </summary> namespace Insus.NET { public class MusicType { private int _ID; private string _TypeName; public int ID { get { return _ID; } set { _ID = value; } } public string TypeName { get { return _TypeName; } set { _TypeName = value; } } public MusicType() { // // TODO: Add constructor logic here // } public MusicType(int id, string typeName) { this._ID = id; this._TypeName = typeName; } } }
填充对象:

public List<MusicType> GetMusicType() { List<MusicType> mt = new List<MusicType>(); mt.Add(new MusicType(1, "甜密情歌")); mt.Add(new MusicType(2, "网络红歌")); mt.Add(new MusicType(3, "儿童歌曲")); mt.Add(new MusicType(4, "民族精选")); mt.Add(new MusicType(5, "校园歌曲")); mt.Add(new MusicType(6, "摇滚音乐")); mt.Add(new MusicType(7, "胎教音乐")); mt.Add(new MusicType(8, "红色名曲")); mt.Add(new MusicType(9, "串烧金曲")); mt.Add(new MusicType(10, "动慢歌曲")); return mt; }
在站点建一个aspx网页,并拉两个控件,一个是CheckBox和CheckBoxList:

全选<asp:CheckBox ID="CheckBoxAll" runat="server" onClick="javascript:Check_Uncheck_All(this);" /><br /> <asp:CheckBoxList ID="CheckBoxListMusicType" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" Width="300"></asp:CheckBoxList>
接下来,我们为CheckBoxList绑定数据:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Insus.NET; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) Data_Binding(); } private void Data_Binding() { this.CheckBoxListMusicType.DataSource = GetMusicType(); this.CheckBoxListMusicType.DataTextField = "TypeName"; this.CheckBoxListMusicType.DataValueField = "ID"; this.CheckBoxListMusicType.DataBind (); } }
最后是写Javascript代码:

<script type="text/javascript"> function Check_Uncheck_All(cb) { var cbl = document.getElementById("<%=CheckBoxListMusicType.ClientID%>"); var input = cbl.getElementsByTagName("input"); if (cb.checked) { for (var i = 0; i < input.length; i++) { input[i].checked = true; } } else { for (var i = 0; i < input.length; i++) { input[i].checked = false; } } } </script>
ok完成,看看效果:
标签:
JavaScript
, checkbox
, js
, CheckBoxList
, input
, List
, getElementById
, getElementsByTagName
, ClientID
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2012-01-20 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突。
2012-01-20 sp_executesql 使用复杂的 Unicode 表达式
2012-01-20 OBJECTPROPERTY与sp_rename更改对象名称
2010-01-20 Generics and Linq demo
2010-01-20 Two ListBox selector demo