Solved

How to toggle checkall uncheckall asp.net checkboxlist javascript?

Posted on 2013-01-06
10
341 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
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…

932 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

14 Experts available now in Live!

Get 1:1 Help Now