Micro
Micro

Reputation: 10891

Child repeater does not exist in current context

I have a nested repeater inside of another repeater like this:

<table>
    <asp:Repeater ID="RepeaterOuter" runat="server">
    <ItemTemplate>
        <tr>
        <td><asp:TextBox Text='<%# Eval("Author") %>' /></td>
        </tr>
        <asp:Repeater ID="RepeaterInner" runat="server">
        <ItemTemplate>
        <tr>
            <td><asp:TextBox Text='<%# Eval("Book") %>' /></td>
            <td><asp:TextBox Text='<%# Eval("PublishDate") %>' /></td> 
            <td><asp:TextBox Text='<%# Eval("Pages") %>' /></td>         
        </tr>
        </ItemTemplate>
        </asp:Repeater>    
    </ItemTemplate>
    </asp:Repeater>
</table>

However, when I try to access the child repeater, RepeaterInner, from my code behind file, it says that it does not exist in the current context. The parent repeater, RepeaterOuter, does however.

I am trying to set up a loop, to loop through my TextBox's in the child repeater but it won't let me access it:

//does not work
foreach (RepeaterItem item in RepeaterInner.Items)
            {
                txtBook= (TextBox)item.FindControl("Book");
                txtPublishDate = (TextBox)item.FindControl("PublishDate");
                txtPages = (TextBox)item.FindControl("Pages");

                // do something....

}

Thank you.

Upvotes: 1

Views: 1955

Answers (1)

Andrei
Andrei

Reputation: 56716

At first, I very much doubt this inner repeater even exists before the outer one is data bound. So make sure you are accessing inner repeater at a right time.

At second, controls that are in templates are not visible like this on the page. To get the control in the template you need to use FindControl. Also note that FindControl works only with direct children, so your code should look something like this:

var innerRepeater = RepeaterOuter.Items[0].FindControl("RepeaterInner") as Repeater;

Upvotes: 3

Related Questions