Accessing parent data in nested repeater, in the HeaderTemplate

亡梦爱人 提交于 2019-11-26 10:56:59

问题


Simple question, not sure there\'s a simple answer!

So here\'s the code: (I\'ve simplified it a lot to make it easier to read)

<asp:Repeater runat=\"server>
    <ItemTemplate>
        <asp:Repeater runat=\"server\">
            <HeaderTemplate>
                <h1>My header here for: <%# OuterContainer.DataItem.MyItemName %> </h1>
            </HeaderTemplate>
            <ItemTemplate>
                My items code here
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

How, in the HeaderTemplate - can I access the DataItem in the parent repeater?


回答1:


I have found the answer actually:

Use:

<HeaderTemplate>
    <%# ((RepeaterItem)Container.Parent.Parent).DataItem %>
</HeaderTemplate>



回答2:


Solution given by Paul didn't work for me, but this did:

<%# DataBinder.Eval(Container.Parent.Parent, "DataItem.YourProperty")%> 



回答3:


This is an old thread, but it seems proper to add:

In my case I have 2 nested ASPxGridView controls (DevExpress) and Container.Parent.Parent didn't work.

To access parent's data item from child, this is what worked for me:

<%# DataBinder.Eval(Container.NamingContainer.NamingContainer, "DataItem.DbField")%>



回答4:


If I want to retrieve a property of a parent repeater I typically do this:

<%# DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "ThePropertyName")%>



回答5:


I have used as below. Two Repeaters act as Parent and Child.below how I get Parent value of ID Column inside Child repeater.

<%# DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "ID") %>


来源:https://stackoverflow.com/questions/1411336/accessing-parent-data-in-nested-repeater-in-the-headertemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!