CheckBox implements full or cancel selection for CheckBoxList (js Code Implementation)

Source: Internet
Author: User

At a certain time, there are too many options for the CheckBoxList. You need a function of Selecting All or canceling all. The following uses Javascript to implement it.
Prepare an object:
MusicType Copy codeThe Code is as follows: 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;
}
}
}

Filling object:Copy codeThe Code is as follows: public List <MusicType> GetMusicType ()
{
List <MusicType> mt = new List <MusicType> ();
Mt. Add (new MusicType (1, "Sweet Love Song "));
Mt. Add (new MusicType (2, "Network hongge "));
Mt. Add (new MusicType (3, "children's songs "));
Mt. Add (new MusicType (4, "Ethnic selection "));
Mt. Add (new MusicType (5, "campus song "));
Mt. Add (new MusicType (6, "Rock Music "));
Mt. Add (new MusicType (7, "prenatal music "));
Mt. Add (new MusicType (8, "Red name song "));
Mt. Add (new MusicType (9, "braised gold "));
Mt. Add (new MusicType (10, "slow songs "));
Return mt;
}

Create An aspx webpage on the site and pull two controls, one being CheckBox and CheckBoxList:Copy codeThe Code is as follows: select all <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>

Next, we bind data to the CheckBoxList:Copy codeThe Code is as follows: 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 ();
}
}

Finally, write Javascript code.:Copy codeThe Code is as follows: <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. Check the effect.:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.