Anders
Anders

Reputation: 12560

Change value of databound control within Repeaters in C#

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!

Upvotes: 1

Views: 7967

Answers (3)

Paul Alan Taylor
Paul Alan Taylor

Reputation: 10680

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.

Upvotes: 3

Spencer Ruport
Spencer Ruport

Reputation: 35117

You need to change that line to

var oRepeater = (Repeater) e.Item.FindControl("listItems");

Upvotes: 1

David Peters
David Peters

Reputation: 1978

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.

Upvotes: 0

Related Questions