Solved

How to toggle checkall uncheckall asp.net checkboxlist javascript?

Posted on 2013-01-06
10
349 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i test my code? 1 36
Checking data with javascript 3 19
Help with removing item from Session Array 3 23
Html form and modal / img src -problem 3 9
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

770 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