locked
Gridview checkbox Javascript RRS feed

  • Question

  • User-49671077 posted

    I have two checkbox In GridVew, when I click checkbox (pr_Select ) the other column checkbox (me_Select) will checked as well, The problem I can uncheck (me_Select) while can't uncheck (pr_Select)

    Can someone look the javascript code and find the problem.

    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="Pr_Header" runat="server" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="Pr_Select" runat="server" onclick="Check_chk(this)"/>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="Me_Header" runat="server"/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="Me_Select" runat="server"/>
    </ItemTemplate>
    </asp:TemplateField>

    <script type = "text/javascript">
    
            function Check_chk(rb) {
                var isChecked = rb.checked;
                var row = rb.parentNode.parentNode;
                if (isChecked) {
                     row.disabled = false;
                  row.checked = false;
                    
                }
    
                var rowItems = row.getElementsByTagName('input');
                for (i = 0; i < rowItems.length; i++) {
                    if (rowItems[i].type == "checkbox") {
                       rowItems[i].checked = true;
                       rowItems[i].disabled = false;
                    }
                }
    
            }
    
    </script>

    Thursday, February 27, 2020 2:05 PM

All replies

  • User1535942433 posted

    Hi kafsar,

    Accroding to your description and codes,I don't understand your requirment clearly.Could you tell us when you click the checkbox(me_Select),the another checkbox could be checked?If you want to uncheck(me_Select),you need to add function on the chechbox(me_Select).I suggest you could add function on chechbox(me_Select).

    Since you don't post more details,I create a demo.

    More details,you could refer to below codes:

     <script type="text/javascript">
            function Check_chk(rb) {
                var isChecked = rb.checked;
                var row = rb.parentNode.parentNode;
                if (isChecked) {
                    row.disabled = false;
                    row.checked = false;
                }
                var rowItems = row.getElementsByTagName('input');
                for (i = 0; i < rowItems.length; i++) {
                    if (rowItems[i].type == "checkbox") {
                        rowItems[i].checked = true;
                        rowItems[i].disabled = false;
                    }
                }
            }
            function Check_unchk(ab)
            {
                var isChecked = ab.checked;
                var row = ab.parentNode.parentNode;
                if (!isChecked)
                {
                    row.disabled = false;
                    row.checked = true;
                }
                 var rowItems = row.getElementsByTagName('input');
                for (i = 0; i < rowItems.length; i++) {
                    if (rowItems[i].type == "checkbox") {
                        rowItems[i].checked = false;
                        rowItems[i].disabled = false;
                    }
                }
            }
        </script>
    
    
    <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="No1" DataSourceID="SqlDataSource1">
                    <Columns>
                        <asp:BoundField DataField="No1" HeaderText="No1" ReadOnly="True" SortExpression="No1" />
                        <asp:BoundField DataField="No2" HeaderText="No2" SortExpression="No2" />
                        <asp:BoundField DataField="No3" HeaderText="No3" SortExpression="No3" />
                        <asp:BoundField DataField="No4" HeaderText="No4" SortExpression="No4" />
                        <asp:BoundField DataField="No5" HeaderText="No5" SortExpression="No5" />
                        <asp:BoundField DataField="No6" HeaderText="No6" SortExpression="No6" />
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="Pr_Header" runat="server" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="Pr_Select" runat="server" onclick="Check_chk(this)" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="Me_Header" runat="server" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="Me_Select" runat="server" onclick="Check_unchk(this)" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-TestApplicationWithDatabase-20190820030542 %>" SelectCommand="SELECT * FROM [ReapterTotal]"></asp:SqlDataSource>
            </div>

    Result:

    Best regards,

    Yijing Sun

    Friday, February 28, 2020 5:50 AM