Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to toggle checkall uncheckall asp.net checkboxlist javascript?

Posted on 2013-01-06
10
Medium Priority
?
436 Views
Last Modified: 2013-09-27
Hi,

I have one checkbox 'check all' which is used to check all the checkboxes and 'check all' text changed to 'uncheck all'. Uncheck all text uncheck al checkboxes and 'uncheck all' text change to 'check all'.

 <form id="form1" runat="server" >
            <asp:CheckBox  ID="check All" CssClass="statesettings" runat="server"   Text="Check All"   />
                                                     
        <asp:CheckBoxList ID="cblStates" runat="server"             DataSourceID="StatesDataSource" DataTextField="StateName"  
            DataValueField="State_ID"  RepeatColumns=4 RepeatDirection="Vertical" RepeatLayout="Table" Width="475px"/>    
          <asp:Button ID="CloseBtn" runat="server" Text="Submit" CssClass="close"  OnClick="Close_Click"   />
</form>
0
Comment
Question by:Saroj13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1500 total points
ID: 38749377
"check All" is not a valid ID, space is not allowed, correct it to checkAll
add this in the header section of your page :

<script type="text/javascript">
$(document).ready(function() {
     $("#<%= checkAll.ClientID =>").change(function(evt) {
          evt.preventDefault();
          var l = $(this).is(":checked")?"Uncheck all":"Check all";
          $("label[for='#<%= checkAll.ClientID =>']").text(l);
     });
})
</script>

Open in new window

0
 
LVL 9

Expert Comment

by:abolinhas
ID: 38749402
HTML:
<input type="button" name="ca_v2_on" value="JQuery Check All myCB" onclick="jqCheckAll('myForm', 'myCB', 1);"/>
2<input type="button" name="ca_v2_off" value="JQuery Uncheck All myCB" onclick="jqCheckAll('myForm', 'myCB', 0);"/>

Jquery:
function jqCheckAll( id, name, flag )
{
    if (flag == 0)
    {
        $("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', false);
    }
    else
    {
        $("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', true);
    }
}

Open in new window


Source & Demo:
http://www.iknowkungfoo.com/blog/index.cfm/2008/7/9/Check-All-Checkboxes-with-JQuery
0
 

Author Comment

by:Saroj13
ID: 38749491
thanks. I tried first solution and it says server tag is not well formed. i have put code inside <head> block.

I want only one checkbox that will do toggle checkall and uncheckall. Toggle will also change the text from checkall to unceckall and viceversa.
0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 82

Expert Comment

by:leakim971
ID: 38749493
<script type="text/javascript" language="javascript">
$(document).ready(function() {
     $("#<%= checkAll.ClientID =>").change(function(evt) {
          evt.preventDefault();
          var l = $(this).is(":checked")?"Uncheck all":"Check all";
          $("label[for='#<%= checkAll.ClientID =>']").text(l);
     });
})
</script>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38749495
<< it >> ?
0
 

Author Comment

by:Saroj13
ID: 38749813
Its not working. checkAll checkbox is not checking all the states checkboxes

<script type="text/javascript" language="javascript">
$(document).ready(function() {
     $('<%= checkAll.ClientID %>').change(function(evt) {
          evt.preventDefault();
          var l = $(this).is(":checked")?"Uncheck all":"Check all";
          $("label[for='<%= checkAll.ClientID %>']").text(l);
     });
})
</script>


 <form id="form1" runat="server" >
        <asp:CheckBox ID="checkAll" runat="server"   Text="check all" />
 
     <asp:FormView id="InsertScriptFormView1"  Runat="Server" DataSourceID="GetDetailDataSource"
           DefaultMode="Insert" Font-Size=Small Font-Names="Arial" Width="100%"
           >  
               <InsertItemTemplate>
           
                                                             
        <asp:CheckBoxList ID="cblStates" runat="server" CssClass="statesettings"
            DataSourceID="StatesDataSource" DataTextField="StateName"  
            DataValueField="State_ID"  RepeatColumns=4 RepeatDirection="Vertical" RepeatLayout="Table" Width="475px"/>    
            <br/>
          <asp:Button ID="CloseBtn" runat="server" Text="Submit" CssClass="close"  OnClick="Close_Click"   />
               
       
   
  </InsertItemTemplate></asp:FormView>
</form>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38749819
the code provided only update the label... it don't do this job?
0
 

Author Comment

by:Saroj13
ID: 38749837
hi,

Please modify the code: Below code does:
1. Check All checkbox click check all the state checkboxes
2. CheckAll checkbox uncheck will uncheck all the state checkboxes.

Please update the code that checkall checkbox text change to uncheckall when all the checkboxes are selected.
function SelectAllCheckboxes(spanChk, str) {
          // Added as ASPX uses SPAN for checkbox     
          var oItem = spanChk.children;
          var theBox = (spanChk.type == "checkbox") ? spanChk : spanChk.children.item[0];
          var control;
          xState = theBox.checked;
          elm = theBox.form.elements;
          for (i = 0; i < elm.length; i++)
              if (elm[i].type == "checkbox" && elm[i].id != theBox.id) {
                  control = elm[i].id.indexOf(str);
                  if (elm[i].checked != xState && control != -1) {
                      elm[i].click();
                  }
              }

             
          }

<form id="form1" runat="server" method="post" action=''>
    <p style="text-align:left; margin-left:20px;">
        <asp:CheckBox  ID="checkAll"  runat="server" onclick="javascript:SelectAllCheckboxes(this,'cblStates');"             Text="Check All"   /> 
   </p>
 
     <asp:FormView id="InsertScriptFormView1"  Runat="Server" DataSourceID="GetDetailDataSource"
           DefaultMode="Insert" Font-Size=Small Font-Names="Arial" Width="100%" 
           >  
               <InsertItemTemplate>
           
                                                             
        <asp:CheckBoxList ID="cblStates" runat="server" CssClass="statesettings" 
            DataSourceID="StatesDataSource" DataTextField="StateName"  
            DataValueField="State_ID"  RepeatColumns=4 RepeatDirection="Vertical" RepeatLayout="Table" Width="475px"/>     
            <br/>
          <asp:Button ID="CloseBtn" runat="server" Text="Submit" CssClass="close"  OnClick="Close_Click"   />
                
        
    
  </InsertItemTemplate></asp:FormView>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 38749847
You did not answer my question... Please confirm the code provided change the label.THANKS
0
 

Author Comment

by:Saroj13
ID: 38749863
This is my final code. Please change the checkbox text checkall to uncheckall in javascript when all the checkboxes are checked.
function SelectAllCheckboxes(spanChk, str) {
          // Added as ASPX uses SPAN for checkbox     
          var oItem = spanChk.children;
          var theBox = (spanChk.type == "checkbox") ? spanChk : spanChk.children.item[0];
          var control;
          xState = theBox.checked;
          elm = theBox.form.elements;
          for (i = 0; i < elm.length; i++)
              if (elm[i].type == "checkbox" && elm[i].id != theBox.id) {
                  control = elm[i].id.indexOf(str);
                  if (elm[i].checked != xState && control != -1) {
                      elm[i].click();
                  }
              }

             
          }

<form id="form1" runat="server" method="post" action=''>
    <p style="text-align:left; margin-left:20px;">
        <asp:CheckBox  ID="checkAll"  runat="server" onclick="javascript:SelectAllCheckboxes(this,'cblStates');"             Text="Check All"   /> 
   </p>
 
     <asp:FormView id="InsertScriptFormView1"  Runat="Server" DataSourceID="GetDetailDataSource"
           DefaultMode="Insert" Font-Size=Small Font-Names="Arial" Width="100%" 
           >  
               <InsertItemTemplate>
           
                                                             
        <asp:CheckBoxList ID="cblStates" runat="server" CssClass="statesettings" 
            DataSourceID="StatesDataSource" DataTextField="StateName"  
            DataValueField="State_ID"  RepeatColumns=4 RepeatDirection="Vertical" RepeatLayout="Table" Width="475px"/>     
            <br/>
          <asp:Button ID="CloseBtn" runat="server" Text="Submit" CssClass="close"  OnClick="Close_Click"   />
                
        
    
  </InsertItemTemplate></asp:FormView>

Open in new window

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question