Click here to Skip to main content
15,118,001 members
Articles / Web Development / ASP.NET
Technical Blog
Posted 21 Aug 2011

Tagged as


8 bookmarked

Playing with List Controls using jQuery

Rate me:
Please Sign up or sign in to vote.
4.97/5 (7 votes)
22 Aug 2011CPOL2 min read
Playing with List Controls using jQuery

You all must be knowing, I am a fan of jQuery. The kind of neat and clean code and the power provided by jQuery at Client side scripting is unmatchable.

List Controls are one of the most used form controls. And we all know that manipulating these controls from JavaScript has always been a tough task.

In this post, I’ll be discussing about accessing List controls and manipulating it with the help of jQuery. Hope you all like it and will be able to apply with other controls as well.

Let’s discuss it for CheckBoxList. I have a checkbox list and for demonstration also, I have created a sample. For this sample, I have created a dictionary with few Items and set it as a datasource of CheckboxList as:

Dictionary dictItems = new Dictionary();
dictItems.Add(1, "Tea");
dictItems.Add(2, "Coffee");
dictItems.Add(3, "Lemon Tea");
dictItems.Add(4, "Expresso");
dictItems.Add(5, "Cappuccino");

chkItems.DataSource = dictItems;
chkItems.DataTextField = "Value";
chkItems.DataValueField = "Key";

Let's also have a look at the aspx code.

<div id="divcheckboxList">
        <asp:checkboxlist ID="chkItems" runat="server"></asp:checkboxlist>

Here you can see that I have put the CheckboxList in a div whose Id is divcheckboxList.

This div will be very helpful while finding the Items in CheckboxList because as you know the narrow search criteria we will pass to the jQuery, it will be as much faster.

Now let's have a look at the rendered code:

<div id="divcheckboxList">
<table id="chkItems" border="0">
<td><input id="chkItems_0" type="checkbox" name="chkItems$0" />
<label for="chkItems_0">Tea</label></td>
<td><input id="chkItems_1" type="checkbox" name="chkItems$1" />
<label for="chkItems_1">Coffee</label></td>
<td><input id="chkItems_2" type="checkbox" name="chkItems$2" />
<label for="chkItems_2">Lemon Tea</label></td>
<td><input id="chkItems_3" type="checkbox" name="chkItems$3" />
<label for="chkItems_3">Expresso</label></td>
<td><input id="chkItems_4" type="checkbox" name="chkItems$4" />
<label for="chkItems_4">Cappuccino</label></td>

Now we’ll see some most important operations with the control using jQuery.

  • Finding all Checkboxes:
//This will return the array of all the checkboxes inside the div divcheckboxList
  • Finding all selected CheckBoxes:
    //This will return the array of all the checkboxes inside the div divcheckboxList
  • Select all selected CheckBoxes:
    //This finds all the checkbox in the div and adds the 
    //checked attribute to each checkbox which shows every checkbox as checked
     $($('#divcheckboxList').find('input:checkbox')).attr('checked', true);
  • Remove Selection of all Selected CheckBoxes:
    //Find all checkBoxes and deselect it by removing checked 
    //attribute to each checkbox (in the div divcheckboxList)
  • Adding event to each CheckBox:
    //Find all checkBoxes and attach a Click function (in the div divcheckboxList)
  • Removing event to each CheckBox:
    //Find all the checkBoxes and remove the attached Click function 
    //(in the div divcheckboxList)
  • Getting Text of selected CheckBox:
    //Find all the selected checkbox and show the text of the checkbox
    var selectedCheckBoxes = $('#divcheckboxList').find
    	('input:checkbox:checked').each(function () {

    As you see in the above code, you might get confused. But if you see the rendered code, you will get to know that every Checkbox is rendered as two controls, one Checkbox input control and another label which holds the text of Checkbox. That’s why to get the text of Checkbox, I had to find the sibling label of the checkbox.

The same will also work for RadioButtonList.

Note: Here I have presented a way to work with List controls and demonstrated it using jQuery. There could be many more ways to do the same with the help of jQuery and JavaScript.

Hope it will help the developers who are new to jQuery.




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.

He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.

He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website

He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.

Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.

Visit his Blog: Code Wala

Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008

Comments and Discussions

GeneralMy vote of 5 Pin
Anurag Gandhi7-Sep-11 5:51
professionalAnurag Gandhi7-Sep-11 5:51 
GeneralRe: My vote of 5 Pin
Brij7-Sep-11 8:48
mentorBrij7-Sep-11 8:48 
GeneralMy vote of 5 Pin
Kunal Chowdhury «IN»30-Aug-11 8:47
professionalKunal Chowdhury «IN»30-Aug-11 8:47 
GeneralRe: My vote of 5 Pin
Brij1-Sep-11 22:27
mentorBrij1-Sep-11 22:27 
SuggestionUsing Div or not using Pin
Jesús Utrera23-Aug-11 23:51
MemberJesús Utrera23-Aug-11 23:51 
GeneralRe: Using Div or not using Pin
Brij26-Aug-11 8:05
mentorBrij26-Aug-11 8:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.