Search code examples
c#asp.netdatalist

asp.net DataList inside Datalist problems with C# interpretation


I need to use DataList inside another DataList. It works fine for me, but when I'm trying to do something in code behind with this inside one it just doesn't exist for C#. Here is code:

...

<asp:DataList ID="DataListDziennik" runat="server"
    DataSourceID="SqlDataSourcePrzedmioty">
    <ItemTemplate>
        <asp:Label ID="LabelPrzedmiot" runat="server" Text='<%# Eval("przedmiot") %>' />

        ...

            <asp:DataList ID="DataListOceny" runat="server" 
                DataSourceID="SqlDataSourceOceny" 
                RepeatDirection="Horizontal" 
                OnItemCommand="DataListOceny_ItemCommandOceny"
                OnEditCommand="DataListOceny_EditCommandOceny">

                <EditItemTemplate>

                    <asp:TextBox ID="TextBoxOcena" runat="server" Text='<%# Bind("lista") %>' />
                    <td><asp:Button ID="ButtonZapisz" CommandName="Update" runat="server" Text="Zapisz" /></td>

                </EditItemTemplate>

                <ItemTemplate>

                    <asp:TextBox Width="20" ID="TextBoxOcena" ReadOnly="true" Text='<%# Eval("lista") %>' runat="server"></asp:TextBox>
                    <td><asp:Button ID="ButtonEdytuj" CommandName="Edit" runat="server" Text="Edytuj" /></td>

                </ItemTemplate>
            </asp:DataList>
        </td>
    </ItemTemplate>
</asp:DataList>

When I write this in code behind:

        protected void DataListOceny_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataListOceny.EditItemIndex = e.Item.ItemIndex;

        DataListOceny.DataBind();
    }

...Visual Studio tells me that DataListOceny does not exist in current content. I just want to be able edit items on DataListOceny after clicking the "edit" button, it can be placed anywhere on website. Do you know any solution for this problem?


Solution

  • Because DataListOceny is a control inside of another control, you have to make a reference to it by doing something like:

    DataList DataListOceny = (DataList)e.Item.FindControl("DataListOceny");
    

    Once you do that, you can use the DataListOceny variable. Hope this helps.