Changing items in the LayoutTemplate of a nested ListView

This is related to the previous question, but I thought I'd simplify it and make a call out of it. Given the code below, can you change the "ChangeThisLabel" value from the code behind?

<asp:ListView ID="OuterListView" runat="server">
    <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
        <%#Eval("outer_value")%> <br/>
        <asp:ListView ID="InnerListView" runat="server" DataSource='<%#Eval("inner") %>'>
            <LayoutTemplate>
                <asp:Label ID="ChangeThisLabel" runat="server" />
                <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
            </LayoutTemplate>
            <ItemTemplate>
                <%#Eval("inner_value")%> <br/>
            </ItemTemplate>
        </asp:ListView>
    </ItemTemplate>
</asp:ListView>

      

I would try it myself before posting the answer, as I got a lot of suggestions in my previous post that work fine for a single ListView, but crash when going against a nested ListView.

0


source to share


2 answers


as mentioned in another answer. in the code behind, on load, you can do this:

`OuterListView.FindControl("InnerListView").FindControl("ChangeThisLabel")

      

then draw it as a label and change the text. obviously you will be iterating over this code inside the loop so that you do it for each shortcut in the internal list view of each list appearance.

And as for the answers to your other question, you didn't quite understand that you want to access it from code. You can also post what you have tried so people know that you have tried different methods.



Good luck!

edit: regarding your comment:

If I'm missing something, it won't work because "ChangeThisLabel" becomes "ctl00_ctl00_BodyContentPlaceHolder_ReportContentPlaceHolder _OuterListView_ctrl0_InnerListView_ChangeThisLabel" when displayed by the internal ListView

you are correct, but when you use FindControl (id) it will use the server side id to find the control. if you do this: InnerListView.FindControl ("ChangeThisLabel") then it will find the correct label regardless of the client side ID assigned to that control.

+3


source


In a DataBound event handler (or something like that) use FindControl("ChangeThisLabel")

to get a reference to the label. You must of course use the link before using the link Label

.



You might want to write your own FindControl method that you call recursively to find the n-level control.

+1


source







All Articles