国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

CheckBox為CheckBoxList實現全選或全取消選擇(js代碼實現)

2020-01-18 00:17:11
字體:
來源:轉載
供稿:網友
某一個時候,CheckBoxList的選擇太多,用戶需要一個全選或全取消的功能。下面使用Javascript來實現它。
準備好一個對象
MusicType
復制代碼 代碼如下:

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完成,看看效果
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 旬邑县| 郎溪县| 万载县| 大城县| 黎川县| 巨野县| 富平县| 文化| 花垣县| 双鸭山市| 枝江市| 山东| 西安市| 布尔津县| 和政县| 衡山县| 政和县| 文化| 连州市| 古交市| 惠州市| 施秉县| 巴彦淖尔市| 蒲江县| 米泉市| 正镶白旗| 蕲春县| 当阳市| 龙泉市| 海南省| 建阳市| 鲁甸县| 宜兰县| 惠东县| 巴林左旗| 镇雄县| 随州市| 德化县| 富平县| 海盐县| 东丰县|