4

I have a nested repeater control. Here is what the HTML looks like:

<ItemTemplate>
    <div class="boxLeft">
        <h4><%# DataBinder.Eval(Container.DataItem, "DisciplineName") %></h4><asp:Label runat="server" ID="lblDisciplineID" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "DisciplineID") %>'></asp:Label>
        <p><%# DataBinder.Eval(Container.DataItem, "DisciplineNarrative") %></p>
        <h5 class="articles"><%# DataBinder.Eval(Container.DataItem, "InstructorCount")%> instructors</h5>
        <ul>
            <asp:Repeater runat="server" ID="rptRegions">
                <ItemTemplate>
                    <li><a href='/Lessons/<%# DataBinder.Eval(Container.DataItem, "DisciplineName") %>/<%# DataBinder.Eval(Container.DataItem, "CityName") %>'><%# DataBinder.Eval(Container.DataItem, "CityName") %></a></li>
                </ItemTemplate>
            </asp:Repeater>
        </ul>
    </div>
</ItemTemplate>

As you can see, I want to access the parent DisciplineName property from the parent repeater, in my child repeater in order to build the URL. I get the following error:

DataBinding: 'GolfLessonSearch.Model.CityEntity' does not contain a property with the name 'DisciplineName'.

This is because it is trying to get "DisciplineName" from the child repeater but I want it from the parent repeater. I thought that the properties may still be in scope but it appears not. Is there any way of getting this?

2
  • 1
    will this work for you? DataBinder.Eval(((RepeaterItem)Container.Parent).DataItem , "DisciplineName") I am not sure whether this will work or not. SO I am putting this as comment instead of answer Commented Jan 4, 2012 at 16:25
  • @Pavan Will need to be Container.Parent.Parent (The first Parent is the Repeater you are in), but otherwise your suggestion works. Commented Jan 28, 2020 at 22:59

1 Answer 1

4

If the objects behind your DataItems have the same parent/child relationship as you are trying to represent them in the repeater, you can always fully qualify the property name so that your child repeater is calling

DataBinder.Eval(Container.DataItem, "Parent.DisciplineName")

If this isn't the case, my gut would be to create a ViewModel object (even though it's not mvc) of your child object to fake that relationship...

EDIT

Just a note that when I said "Parent.DisciplineName", I meant to replace "Parent" with the object name... (I only qualify because "Parent" is a reserved word in so many other places in asp.net...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.