Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have a GridView having a check box column.I want to check uncheck these check boxes serially i.e.a row can not be checked if its previous row is unchecked..similarly if a row is unchecked all rows next to it will be unchecked using JavaScript..


Waiting for Reply.
Thanks in Advance
Posted
Updated 28-Dec-11 23:12pm
Comments
Wendelius 29-Dec-11 5:13am    
Capital letters corrected from the title. Don't use all capital text, it's considered as shouting.

 
Share this answer
 
v2
using javascript, find object of grid and find rows.count of grid.
using for loop enable or disable check boxes of grid with using comparision with selecting index.
 
Share this answer
 
Comments
Deepak.xip 29-Dec-11 5:25am    
Send me the complete code dude.I dont know Javascript very well.
You can try this,

XML
<asp:GridView ID="gvBranches" runat="server" AutoGenerateColumns="False" CellPadding="4"
        Font-Names="Verdana" Font-Size="11px" GridLines="None"
        Width="182px">
        <Columns>
            <asp:TemplateField HeaderText="Check_All">
                <HeaderTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" onclick="checkAll(this);" />
                </HeaderTemplate>
                <ItemStyle HorizontalAlign="Left" Width="30px" />
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox3" runat="server" onclick="Check_Click(this)" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Column">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text="Some Value"></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
        </Columns>
    </asp:GridView>



And js functions are like,

C#
<script type="text/javascript">
        function checkAll(objRef) {
            var GridView = objRef.parentNode.parentNode.parentNode;
            var inputList = GridView.getElementsByTagName("input");
            var c = 0;
            for (var i = 0; i < inputList.length; i++) {
                var row = inputList[i].parentNode.parentNode;
                if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
                    if (objRef.checked) {
                        c = 1;
                        inputList[i].checked = true;
                    }
                    else {
                        c = 0;
                        inputList[i].checked = false;
                    }
                }
            }

        }
        function Check_Click(objRef) {
            var row = objRef.parentNode.parentNode;
            var GridView = row.parentNode;
            var inputList = GridView.getElementsByTagName("input");
            var c = 0;
            inputList[0].checked = true;
            for (var i = 1; i < inputList.length; i++) {
                if (inputList[i].type == "checkbox" && inputList[i].checked == true) {
                    c = 1;
                }
                else {
                    inputList[0].checked = false;
                }
            }

        }
    </script>
 
Share this answer
 
v2
Comments
Deepak.xip 30-Dec-11 0:40am    
Didn't work.Its only for Check and Uncheck all...

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