Click here to Skip to main content
15,748,559 members
Articles / Web Development / HTML
Technical Blog
Posted 4 Jun 2011


3 bookmarked

ASP.NET - Get selected CheckBox value in DataList

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
4 Jun 2011CPOL1 min read
How to retrieve a checkbox value from an ASP.NET DataList control


A while ago, someone at the forum had asked "how to retrieve the checkbox value from the ASP.NET DataList control". I took the opportunity to put together a sample code to achieve the requirement through the Code Behind and jQuery.

In this example, I'm using HTML Input (CheckBox) control in the DataList. Shown in listing 1 is the code used to get the selected checkbox value. The code will loop through the DataListItemCollection, find the CheckBox control by ID, then do something if it was checked.

Listing 1
void GetCheckedBox()
        foreach (DataListItem li in BooksDataList.Items)
            HtmlInputCheckBox cb = li.FindControl("FavChkBox") as HtmlInputCheckBox;

            if (cb != null)
                if (cb.Checked)
                    if (LblText.Text.Length > 0)
                        LblText.Text += ", ";

                    LblText.Text += cb.Value;

Let's say we decide not to write code to loop through the DataListItemCollection, we can collect the selected CheckBox value through the client-side script. Shown in Listing 2 is the jQuery script used to collect the selected CheckBox value. When the user hits the submit button, the script will retrieve all the CheckBox values in the DataList control, stored in an array and later save it to a hidden field.

Listing 2
<script type="text/javascript">
        $(document).ready(function () {
            //check all
             function () {

            $("[id$='btnSubmit']").click(function () {
                var ISBN = [];

		input[type=checkbox]:checked").each(function () {


In the code behind, we can retrieve the hidden field value like this "HiddenField1.Value;"


I hope someone will find this information useful and that it will make your programming job easier. If you find any bugs or disagree with the contents or want to help improve this article, please drop me a line and I'll work with you to correct it. I would suggest downloading the demo and exploring it in order to grasp the full concept of it because I might miss some important information in this article. Please send me an email if you want to help improve this article.

Watch This Script in Action


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

Written By
Software Developer (Senior)
United States United States
I have over 10 years of experience working with Microsoft technologies. I have earned my Microsoft Certified Technology Specialist (MCTS) certification. I'm a highly motivated self-starter with an aptitude for learning new skills quickly.

Comments and Discussions

-- There are no messages in this forum --