Solved

How to toggle checkall uncheckall asp.net checkboxlist javascript?

Posted on 2013-01-06
10
332 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
  • 5
  • 4
10 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
"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
Comment Utility
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
Comment Utility
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
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
<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
Comment Utility
<< it >> ?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Saroj13
Comment Utility
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
Comment Utility
the code provided only update the label... it don't do this job?
0
 

Author Comment

by:Saroj13
Comment Utility
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
Comment Utility
You did not answer my question... Please confirm the code provided change the label.THANKS
0
 

Author Comment

by:Saroj13
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now