views:

1532

answers:

3

Hey SO,

I have a nested repeater control that displays a list of data, in my case it is an FAQ list. here is the design portion:

<asp:Repeater ID="lists" runat="server">
    <ItemTemplate>
        <h2 class="sf_listTitle"><asp:Literal ID="listTitle" runat="server"></asp:Literal></h2>

        <p class="sf_controlListItems">
            <a id="expandAll" runat="server">
                <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:ExpandAll %>"></asp:Literal>
            </a>
            <a id="collapseAll" runat="server" style="display:none;">
                <asp:Literal ID="Literal2" runat="server" Text="<%$Resources:CollapseAll %>"></asp:Literal>
            </a>
        </p>

        <ul class="sf_expandableList" id="expandableList" runat="server">
            <asp:Repeater ID="listItems" runat="server">
                <HeaderTemplate>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <h1 id="headlineContainer" runat="server" class="sf_listItemTitle">
                            <a id="headline" runat="server" title="<%$Resources:ClickToExpand %>"></a>
                        </h1>
                        <div id="contentContainer" runat="server" class="sf_listItemBody" style="display:none;">
                            <asp:Literal ID="content" runat="server"></asp:Literal>
                        </div>
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                </FooterTemplate>
            </asp:Repeater>
        </ul>
    </ItemTemplate>
</asp:Repeater>

The repeater that I am interested in is the second repeater, listItems. In my code-behind, I cannot directly call listItems and see the controls inside of it. I tried to grab the control inside of list.DataBinding (maybe I need to use a different event?) method:

void lists_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var oRepeater = (Repeater) lists.FindControl("listItems");
}

but this comes up as null. Can anyone give me some pointers/tips of what I need to do to gain access to the listItems repeater and it's children controls?

Thanks!

+1  A: 

You need to change that line to

var oRepeater = (Repeater) e.Item.FindControl("listItems");
Spencer Ruport
Thanks for that, I completely forgot haha. So once I get this oRepeater, how do I get the control literal?
Anders
Think I've got you covered in my response now, sir :)
Paul Alan Taylor
Anders you can add the attribute OnItemDataBinding="listItems_ItemDataBound" to the listItems repeater tag. You don't need to iterate through all it's items.
Spencer Ruport
+2  A: 
lists

belongs to each RepeaterItem, not directly to the Repeater itself.

Try :-

void lists_ItemDataBound(object sender, RepeaterItemEventArgs e)
{


    if ( e.Item.ItemType == ListItemType.AlternatingItem 
        || e.Item.ItemType == ListItemType.Item )
    {
       Repeater oRepeater = (Repeater)e.Item.FindControl("listItems");

       // And to get the stuff inside.
       foreach ( RepeaterItem myItem in oRepeater.Items )
       {
          if ( myItem.Item.ItemType == ListItemType.AlternatingItem 
              || myItem.Item.ItemType == ListItemType.Item )  
          {
             Literal myContent = (Literal)myItem.FindControl("content");

             // Do Something Good!
             myContent.Text = "Huzzah!";

          }
       }
    }
}

And you should be good :)

Edited to incorporate DavidP's helpful refinement.

Paul Alan Taylor
Thanks, works great :D
Anders
No probs. The ASP .NET controls hierarchy is lovely once you get to know it :)
Paul Alan Taylor
A: 

You're close! Inside your event handler check the RepeaterItemEventArgs for what kind of row you're dealing with. Your child repeater will only be available on (Alt)Item rows, not headers or footers. My guess is that it's blowing up on the header.

David Peters