1

This is the design for the GridView.

<asp:GridView ID="gmainrole" runat="server" OnRowDataBound="gmainrole_RowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <input type="checkbox" id="chkavail" runat="server" checked='<%#Eval("checkstatus") %>' />
                &nbsp;
                <asp:Literal ID="litstate" runat="server" Text='<%#Eval("areaname") %>'></asp:Literal>
                <asp:Literal ID="lituserrole" runat="server" Text='<%#Eval("nid") %>' Visible="false"></asp:Literal>
                <asp:GridView ID="subrole" runat="server" AutoGenerateColumns="false" OnRowDataBound="subrole_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="chk1vail" runat="server" Checked='<%#Eval("checkstatus") %>' />
                                <asp:Literal ID="litstate" runat="server" Text='<%#Eval("areaname") %>'></asp:Literal>
                                <asp:Literal ID="lituserrole" runat="server" Text='<%#Eval("nid") %>' Visible="false"></asp:Literal>
                                <asp:DataList ID="glastrole" runat="server" GridLines="None" AutoGenerateColumns="false" OnItemDataBound="glastrole_ItemDataBound">
                                    <ItemTemplate>
                                        <div>
                                            <asp:CheckBoxList runat="server" ID="chklastrole">
                                            </asp:CheckBoxList>
                                            &nbsp;
                                                                                            <asp:Literal ID="Literal1" runat="server" Text='<%#Eval("areaname") %>'></asp:Literal>
                                            <asp:Literal ID="litlast" runat="server" Text='<%#Eval("nid") %>' Visible="false"></asp:Literal>
                                            <asp:DataList ID="ecounter" runat="server" GridLines="None" AutoGenerateColumns="false">
                                                <ItemTemplate>
                                                    <asp:CheckBoxList runat="server" ID="chklastrole">
                                                    </asp:CheckBoxList>

                                                    &nbsp;
                                                                                            <asp:Literal ID="Literal11" runat="server" Text='<%#Eval("areaname") %>'></asp:Literal>
                                                    <asp:Literal ID="litlast1" runat="server" Text='<%#Eval("nid") %>' Visible="false"></asp:Literal>
                                                </ItemTemplate>
                                            </asp:DataList>
                                        </div>
                                    </ItemTemplate>
                                </asp:DataList>
                                </table>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

This GridView is a four level nested GridView where the lower two levels are DataLists and upper two levels are GridViews. I want that when a user checks a CheckBox inside the second GridView named subrole(level 2), all the subsequent CheckBoxes in the DataList glastrole(level 3) and DataList ecounter(level 4) get their CheckBoxes checked for that particular level 2 checkbox. I would give you the code that I have tried till now but the fact is I don't even know how to approach this problem. Please help!

0

1 Answer 1

1

Try this can solve your problem:

Add OnCheckedChanged property <asp:CheckBox ID="chk1vail" OnCheckedChanged="Checked_Changed" runat="server" Checked='<%#Eval("checkstatus") %>' />

code behind to find the controls:

    protected void Checked_Changed(object sender, EventArgs e)
    {
        // gets a row from GridView 
        GridViewRow subrole_row = ((CheckBox)sender).Parent as GridViewRow;

        // get GridView 
        GridView subrole = subrole_row.Parent as GridView;

        // find the datalist in GridView 
        DataList glastrole = subrole.Rows[subrole_row.RowIndex].FindControl("glastrole") as DataList;

        glastrole.DataSource = glastrole_dt; // set your data to datalist
        glastrole.DataBind(); // bind datalist

        subrole.DataSource = subrole_dt; // set your data to gridview
        subrole.DataBind(); // bind gridview
    }

    protected void glastrole_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            GridViewRow subrole_row = ((DataList)sender).Parent as GridViewRow;

            CheckBox chk1vail = subrole_row.FindControl("chk1vail") as CheckBox;

            // find CheckBoxList in glastrole
            CheckBoxList chklastrole = e.Item.FindControl("chklastrole") as CheckBoxList;

            // find if checkbox is checked
            if (chk1vail.Checked)
            {
                // here you can check all the CheckBoxList items
                foreach (ListItem item in chklastrole.Items)
                {
                    item.Selected = true;
                }
            }


            // find DataList in glastrole
            DataList ecounter = e.Item.FindControl("ecounter") as DataList;

            ecounter.DataSource = ecounter_dt; // set your data to datalist
            ecounter.DataBind(); // bind datalist
        }
    }

    protected void ecounter_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            DataList glastrole = ((DataList)sender).Parent as DataList;
            DataList ecounter = glastrole.Parent as DataList;

            GridViewRow subrole_row = ecounter.Parent as GridViewRow;

            CheckBox chk1vail = subrole_row.FindControl("chk1vail") as CheckBox;

            // find CheckBoxList in glastrole
            CheckBoxList chklastrole = e.Item.FindControl("chklastrole") as CheckBoxList;

            // find if checkbox is checked
            if (chk1vail.Checked)
            {
                // here you can check all the CheckBoxList items
                foreach (ListItem item in chklastrole.Items)
                {
                    item.Selected = true;
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.