Marius Vasile

Marius Vasile

  • 593
  • 1.9k
  • 147.8k

Nested data list command not working as intended

Nov 11 2024 8:21 AM

I have three nested datalists. On item command from first datalist thigs works well. On item command from second datalist things are not working well. How it should be done, please?

<div id="DivP" runat="server">
    <div class="row no-gutters">
        <asp:DataList ID="DataList1" runat="server" Style="width: 100%" OnItemCommand="DataList1_ItemCommand" DataKeyField="IdOB">
            <ItemTemplate>
                <asp:TextBox ID="txtIDOB" class="hidden" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"IdOB")%>' />
                <asp:LinkButton ID="LinkButton10" runat="server" Text='<%#Eval("Obiectiv")%>' CommandName="Obiectiv" UseSubmitBehavior="false" Style="font-size: 11px;background-color:lightgreen" class="form-control form-control-sm" />

                <div id="DivS" runat="server" class="hidden">
                    <hr style="border: 1px solid #800080" />
                    <asp:DataList ID="DataList2" runat="server" Style="width: 100%" OnItemCommand="DataList2_ItemCommand" DataKeyField="IdCL">
                        <ItemTemplate>
                            <asp:TextBox ID="txtIDCL" class="hidden" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"IdCL")%>' />
                            <asp:LinkButton ID="LinkButton11" runat="server" Text='<%#Eval("Cladire")%>' CommandName="Cladire" UseSubmitBehavior="false" Style="font-size: 11px;background-color:lightsalmon" class="form-control form-control-sm" />

                            <div id="DivT" runat="server" class="hidden">
                                <hr style="border: 1px solid #800080" />
                                <asp:DataList ID="DataList3" runat="server" Style="width: 100%" OnItemCommand="DataList3_ItemCommand" DataKeyField="IdCA">
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtIDCA" class="hidden" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"IdCA")%>' />
                                        <asp:LinkButton ID="LinkButton11" runat="server" Text='<%#Eval("Camera")%>' CommandName="Camera" UseSubmitBehavior="false" Style="font-size: 11px;background-color:lightblue" class="form-control form-control-sm" />
                                    </ItemTemplate>
                                </asp:DataList>
                                <hr style="border: 1px solid #800080" />
                            </div>
                        </ItemTemplate>
                    </asp:DataList>
                    <hr style="border: 1px solid #800080" />
                </div>
            </ItemTemplate>
        </asp:DataList>
    </div>
</div>

and code behind

protected void DataList1_ItemCommand(Object source, DataListCommandEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        int idob = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());

        DataList innerDataList = e.Item.FindControl("DataList2") as DataList;
        innerDataList.DataSource = GetDataTable("SELECT DISTINCT IdCL,Cladire FROM tblCladire WHERE IdOB ='" + idob + "'");
        innerDataList.DataBind();

        HtmlControl theDivS = (HtmlControl)e.Item.FindControl("DivS");
        theDivS.Attributes["class"] = theDivS.Attributes["class"].Replace("hidden", "").Trim();
    }
}
protected void DataList2_ItemCommand(Object source, DataListCommandEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView dataRowView = e.Item.DataItem as DataRowView;
        string idcl = dataRowView["IdCL"].ToString();
        DataList secondDataList = e.Item.FindControl("DataList3") as DataList;

        secondDataList.DataSource = GetDataTable("SELECT DISTINCT IdCA,Camera FROM tblCamera WHERE IdCL ='" + idcl + "'");
        secondDataList.DataBind();

        HtmlControl theDivT = (HtmlControl)e.Item.FindControl("DivT");
        theDivT.Attributes["class"] = theDivT.Attributes["class"].Replace("hidden", "").Trim();
    }
}

 


Answers (5)