SIGN UP MEMBER LOGIN:    
ARTICLE

ASP.NET - Get Selected CheckBox Value in DataList

Posted by Bryian Tan Articles | ASP.NET Programming June 03, 2011
ASP.NET - Get selected CheckBox value in DataList using code behind and jQuery
Reader Level:
 

A while ago, someone in the forum asked "how to retrieve the CheckBox value from the ASP.NET DataList control". I took the opportunity to put together sample code to do that using Code Behind and jQuery.

In this example, I'm using a 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 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, store them in an array and later save the array to a hidden field.

Listing 2

<script type="text/javascript">

        $(document).ready(function () {

            //check all

            $("[id$='chkAll']").click(

             function () {

                 $("INPUT[type='CheckBox']").attr('checked', $("[id$='chkAll']").is(':checked'));

             });

 

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

                var ISBN = [];

                $("[id$='BooksDataList'] input[type=CheckBox]:checked").each(function () {

                    ISBN.push($(this).val());

                });

                $("[id$='HiddenField1']").val(ISBN);

            });

        });

    </script>

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

Conclusion

I hope someone will find this information useful and 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 to grasp the full concept of it because I might have missed some important information in this article. Please send me an email if you want to help improve this article.

Watch this script in action


Demo

Downloads

Download

Login to add your contents and source code to this article
share this article :
post comment
 

how can i set a count for each checkbox and count the number of times it has been selected (eg) i have to create a PROGRAM that will enable user to vote for a candidate and it has to add a count to each candidate that has been selected please help me if U can

Posted by sultan kadanyo Aug 18, 2011

ho can i set a count for each checkbox and count the number of times it has been selected (eg) i have to create a PROGRAM

Posted by sultan kadanyo Aug 18, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor