0

I basically want to ask this question(How to implement "select all" check box in HTML?) but from a asp.net point of view. There seems to be more challenges to over come when you are using asp.net to do this. The CssClass attribute generates a span container with the class you specified and it doesn't get placed on the input. Along with the challenge of masterpages and controls. I am looping through records and displaying them with a checkbox. I was hoping to grab all the checkboxes by class to perform the check all. I don't think that will be possible. Any advice?

Markup:

  <asp:CheckBox runat="server" ID="checkAll" CssClass="CheckAll" />


<asp:Table ID="tblitems" Visible="false" Width="80%" HorizontalAlign="Center" runat="server">
                <asp:TableRow>
                   //The data gets added as a table row.
                </asp:TableRow>
            </asp:Table>`

Browser:

//Check All check box
       <span class="CheckAll"><input id="ctl00_ContentPlaceHolder1_checkAll" type="checkbox" name="ctl00$ContentPlaceHolder1$checkAll" /></span>

//Each checkbox that will be checked looks like this
<span class="chkBox"><input id="ctl00_ContentPlaceHolder1_ctl01" type="checkbox" name="ctl00$ContentPlaceHolder1$ctl01" /></span>

JavaScript

  $('.CheckAll').click(function (event) {
            alert("start");
            if (this.checked) {
                // Iterate each checkbox
                $(':checkbox').each(function () {
                    this.checked = true;
                });
                alert("end");
            }
        });
Community
  • 1
  • 1
jackncoke
  • 2,000
  • 6
  • 25
  • 66

5 Answers5

0

ClientIDMode property of the checkbox control will allow you to more easily work with client-side selectors, like this:

Markup:

<asp:CheckBox runat="server" ID="checkAll" CssClass="CheckAll" ClientIDMode="Static" />

Check out the Control.ClientIDMode Property MSDN documentation.

Note: ClientIDMode is available in ASP.NET 4.0 and later.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

Since each checkbox is in a span with a class 'chkBox', locate the checkboxes using that selector on the click handler:

$('.CheckAll').on('click', function (event) {
    var checked = $(this).prop('checked');
    $('.chkBox :checkbox').prop('checked', checked);
});

It would be more precise if you wrapped all the checkboxes you'd like to have checked in a container div:

<div id="myCheckboxes">
    // .NET code here
</div>

JS:

$('.CheckAll').on('click', function (event) {
    var checked = $(this).prop('checked');
    $('#myCheckboxes :checkbox').prop('checked', checked);
});
Chris Camaratta
  • 2,729
  • 22
  • 35
0

Karl, I believe, has improved the approach. However, if you want to stick with what you have, modify your Javascript to be the following:

$('.CheckAll').find(':checkbox').click(function (event) {
    alert("start");
    if (this.checked) {
        // Iterate each checkbox
        $(':checkbox').each(function () {
            this.checked = true;
        });
        alert("end");
    }
});
0

The following code basically selectes all checkboxes, and change the check statuses based on the CheckAll Checkbox.

enter image description here

<script type="text/javascript">
    $(document).ready(function () {
        $('#<%= CheckAll.ClientID %>').click(function() {
            var checkedStatus = this.checked;
            $("input[type='checkbox']").attr('checked', checkedStatus);
        });
    });
</script>

<asp:CheckBox runat="server" ID="CheckAll" />
<asp:CheckBox runat="server" ID="Check1" />
<asp:CheckBox runat="server" ID="Check2" />
<asp:CheckBox runat="server" ID="Check3" />
<asp:CheckBox runat="server" ID="Check4" />

Multiple Group of CheckBoxes in a Single Page

If you have multiple groups of checkboxes in a single page, you can differentiate them with class.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
    Inherits="WebApplication2012.WebForm1" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            // Selects all enabled checkboxes
            $("#<%= CheckAll.ClientID %>").click(function () {
            var checkedStatus = this.checked;
            $(".myCheckBox input[type='checkbox']").attr('checked', checkedStatus);
        });
    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:CheckBox runat="server" ID="CheckAll" Text="Check All" CssClass="myCheckAll" /><br />
        <asp:CheckBox runat="server" ID="Check1" Text="Check1" CssClass="myCheckBox" /><br />
        <asp:CheckBox runat="server" ID="Check2" Text="Check2" CssClass="myCheckBox" /><br />
        <asp:CheckBox runat="server" ID="Check3" Text="Check3" CssClass="myCheckBox" /><br />
        <asp:CheckBox runat="server" ID="Check4" Text="Check4" CssClass="myCheckBox" />
    </form>
</body>
</html>
Win
  • 61,100
  • 13
  • 102
  • 181
  • This targets the outter span tags. If you view page sources and look at the id provided by `#<%= CheckAll.ClientID %>` its for the parent span not the checkbox. – jackncoke Jul 18 '13 at 14:26
  • I do not understand your comment. As far as I tested the above code, they both work fine. Which part doesn't work for you? – Win Jul 18 '13 at 14:41
  • I can't get it to fire. When i view the source and saw the id of the parent tag. – jackncoke Jul 18 '13 at 14:49
  • It might be something to do with other codes in the page. I uploaded the entire code. So please create a new `Web Form` page without a master page, and debug it. In addition, you want to use Chrome browser console to see whether you have a script error. – Win Jul 18 '13 at 15:23
  • I think it has to do with existing code as well. I just got it to work though using this `function toggle(source) { checkboxes = document.getElementsByTagName('input'); for (var i = 0, n = checkboxes.length; i < n; i++) { checkboxes[i].checked = source.checked; } }` – jackncoke Jul 18 '13 at 15:27
  • In javascript, if a top level code has an error, it will kill the rest of the code. You can use chrome browser's console to detect the bad code. – Win Jul 18 '13 at 15:30
-1

I got it to work using this script below. This not work for every scenario but it worked very well for mine so far. Thanks!

mark up

<asp:CheckBox runat="server" ID="CheckAll" OnClick="toggle(this)" />

Javascript

  function toggle(source) {


                checkboxes = document.getElementsByTagName('input');

                for (var i = 0, n = checkboxes.length; i < n; i++) {
                    checkboxes[i].checked = source.checked;
                }

            }
jackncoke
  • 2,000
  • 6
  • 25
  • 66