Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<script type = "text/javascript">
    function CheckAll(obj) {
        var list = document.getElementById("<%=dlStudentList.ClientID%>");
        var chklist = list.getElementsByTagName("input");
        for (var i = 0; i < chklist.length; i++) {
            if (chklist[i].type == "checkbox" && chklist[i] != obj) {
                chklist[i].checked = obj.checked;
            }
        }
    }
</script>



<asp:DataList ID="dlStudentList" RepeatColumns="4" runat="server">
    <HeaderTemplate>
         <asp:CheckBox id="ChkAll" runat="server" Text="  Select All" 
            onclick = "CheckAll(this);"/>                           
        <br />
                                     
    </HeaderTemplate>    
        <ItemTemplate>
            <label>
                <table cellpadding="0" cellspacing="0" class="cntls checkliststud" style="font-family: Verdana;
                    font-size: 10px; background-color: #f0f0f0; width: 200px; cursor: pointer">
                    <tr>                       
                        <td>                        
                            <input type="checkbox" id="chkRegno" name="chkRegno"  runat="server"  value='<%# Eval("usercode") %>'
                                onfocus="javascript:setFocus(this)" onblur="javascript:onLostFocus(this)"  önkeypress="javascript:test(event,this)" />         
                        </td>
                    </tr>
                </table>
                 
            </label>
        </ItemTemplate>
    </asp:DataList>
Posted
Updated 17-Feb-14 23:54pm
v5

Well, you do nothing for that

A solution with a bit help of jQuery

C#
$(function () {
    $("#ChkAll").change(function () {
        if ($(this).is(':checked')) {
            $(".checkliststud INPUT[type='checkbox']").prop("checked", true);
        } else {
            $(".checkliststud INPUT[type='checkbox']").prop("checked", false);
        }
    });
    $(".chkRow INPUT[type='checkbox']").change(function () {
        $("#ChkAll").prop("checked", false);
    });
});
 
Share this answer
 
Comments
[no name] 18-Feb-14 6:23am    
this is not working ..
Actually on the click of chkAll - all the checkboxes is getting checked and also un-checked..
But what i wanted is when i un-check any checkbox , chkAll should also un-check which is not happening hope u understood
pjaar89 18-Feb-14 7:20am    
Believe me the second part does the thing

$(".checkliststud INPUT[type='checkbox']").change(function () {
$("#ChkAll").prop("checked", false);
});
pjaar89 18-Feb-14 7:35am    
Here is a fiddle that showing my code that actually works

http://jsfiddle.net/pjaaar/r3JdS/
[no name] 18-Feb-14 13:24pm    
<script type = "text/javascript">
function CheckAll(obj) {
var list = document.getElementById("<%=dlStudentList.ClientID%>");
var chklist = list.getElementsByTagName("input");
for (var i = 0; i < chklist.length; i++) {
if (chklist[i].type == "checkbox" && chklist[i] != obj) {
chklist[i].checked = obj.checked;
}
}
}

function uncheck(obj) {
var headerchk = document.getElementById("ctl00_dlStudentList_ChkAll");
headerchk.checked = false;
}
</script>



Finally i got this !! as u have done like that
pjaar89 19-Feb-14 3:12am    
great
Hi,

Please try this hope it work for you

JavaScript
var checkboxes = new Array();
checkboxes = $('input[id*="chkRegno"]');

for (var i = 0; i &lt; checkboxes.length; i++) {
    checkboxes[i].checked = obj.checked;
}


[edit]Code block added[/edit]
 
Share this answer
 
v3
Comments
[no name] 18-Feb-14 6:23am    
this is not working .. Actually on the click of chkAll - all the checkboxes is getting checked and also un-checked.. But what i wanted is when i un-check any checkbox , chkAll should also un-check which is not happening hope u understood
Arav Pradeep Gupta 18-Feb-14 6:38am    
okay , do one thing please apply a css class on chkRegno give any name suppose u have applied css ABC then here is solution to get as u looking for
/----------
$(function () {
$(".ABC").change(function () {
if($("#ChkAll").is(':checked')){
$("#ChkAll").prop("checked", false);
}
});
});

[no name] 18-Feb-14 6:41am    
I have not used any css..
[no name] 18-Feb-14 6:41am    
you can see in my code
Arav Pradeep Gupta 18-Feb-14 6:52am    
please read comment you will apply a dummy class which will use only for you. for now you can give class name ABC same as I have done. It will work definitely .
XML
<pre lang="xml">&lt;script type = &quot;text/javascript&quot;&gt;
    function CheckAll(obj) {
        var list = document.getElementById(&quot;&lt;%=dlStudentList.ClientID%&gt;&quot;);
        var chklist = list.getElementsByTagName(&quot;input&quot;);
        for (var i = 0; i &lt; chklist.length; i++) {
            if (chklist[i].type == &quot;checkbox&quot; &amp;&amp; chklist[i] != obj) {
                chklist[i].checked = obj.checked;
            }
        }
    }

function uncheck(obj) {
            var headerchk = document.getElementById("ctl00_dlStudentList_ChkAll");
            headerchk.checked = false;
        }
&lt;/script&gt;</pre>


<pre>&lt;asp:DataList ID="dlStudentList" RepeatColumns="4" runat="server"&gt;
    &lt;HeaderTemplate&gt;
        &nbsp;&lt;asp:CheckBox id="ChkAll" runat="server" Text="  Select All"
            onclick = "CheckAll(this);"/&gt;
        &lt;br /&gt;
        &nbsp;&nbsp;
    &lt;/HeaderTemplate&gt;
        &lt;ItemTemplate&gt;
            &lt;label&gt;
                &lt;table cellpadding="0" cellspacing="0" class="cntls checkliststud" style="font-family: Verdana;
                    font-size: 10px; background-color: #f0f0f0; width: 200px; cursor: pointer"&gt;
                    &lt;tr&gt;
                        &lt;td&gt;
                            &lt;input type="checkbox" id="chkRegno" name="chkRegno"  runat="server"  value='&lt;%# Eval("usercode") %&gt;'
                                onfocus="javascript:setFocus(this)" onblur="javascript:onLostFocus(this)"  önkeypress="javascript:test(event,this)" onclick=" uncheck();" /&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;

            &lt;/label&gt;
        &lt;/ItemTemplate&gt;
    &lt;/asp:DataList&gt;</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900