views:

1835

answers:

2

This is related to my earlier question, but I thought I'd simplify it and make a challenge out of it. Given the code below, can you change the value of "ChangeThisLabel" 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 suggest trying it yourself before submitting an answer, as I got a lot of suggestions in my earlier post that work fine for a single ListView, but fall down when going up against the nested ListView.

+1  A: 

In your DataBound (or something like it) event handler use FindControl("ChangeThisLabel") to get a reference to the label. Of course, you should cast to Label before using the reference.

You may want to write your own FindControl method that you'll call recursively to find the control at level n.

Kon
Unless I'm missing something, this will not work, because "ChangeThisLabel" becomes "ctl00_ctl00_BodyContentPlaceHolder_ReportContentPlaceHolder_OuterListView_ctrl0_InnerListView_ChangeThisLabel" when it's rendered by the inner ListView.
gfrizzle
My bad - I wasn't stepping through the items collection to find the InnerListView, which is why I wasn't having any luck.
gfrizzle
+3  A: 

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

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

then cast it as a label and change the text. obviously you would iterate this code inside a loop so you do it for every label in ever inner list view of every outside list view.

And regarding answers to your other question, you were not clear that you wanted to access it from the code behind. Also you might want to post what you have tried, so that people know that you have tried different methods.

good luck!

edit: regarding your comment:

Unless I'm missing something, this will not work, because "ChangeThisLabel" becomes "ctl00_ctl00_BodyContentPlaceHolder_ReportContentPlaceHolder _OuterListView_ctrl0_InnerListView_ChangeThisLabel" when it's rendered by the inner ListView

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

Victor
Thanks for pointing me in the right direction. My problem was that I was trying to find InnerListView by doing OutListView.FindControl("InnerListView"), forgetting that InnerListView lived on each row (item) in OuterListView, so looping through the items collection got me there.
gfrizzle
no problem. good luck!
Victor