开发者

How to get parent datasource Eval() inside a nested Repeater? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Accessing parent data in nested repeater, in the HeaderTemplate

I have a nested repeater being databound... let's say the top level repeater is an OrderRow and the inner repeaters are bound to LineItem rows from my DB. ASPX is something like this:

<asp:Repeater ID="rptOrder" runat="server">
    <ItemTemplate>
        <%# Eval("OrderID") %>:<br/>
        <asp:Repeater ID="rptLineItems" runat="server">
            <ItemTemplate>
                <%# Eval("SomeColumn1"); %>
                <%# Eval("SomeColumn2"); %>
                <%# Eval("SomeColumn3"); %>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAddLine" runat="server" CommandArgument=<%# ???? %> />
            </FooterTemplate>
        </asp:Repeater>
    </ItemTemplate
开发者_运维技巧</asp:Repeater>

Now the button on the inner footer will be used to add a new line item... but the command argument needs to be the OrderID from the outer repeater, so we know which order to add to. Obviously a regular Eval() call won't work here, because it will have the inner repeater's DataRowView as a source [actually, it won't, since it's in the footer]. How do I get this value? Am I going to have to set this dynamically in the ItemDataBound event of the outside repeater?


Since the call are serial you can use the code behind to save the last order id and use it later.

Here is the idea.

<asp:Repeater ID="rptOrder" runat="server">
    <ItemTemplate>
        <%#GetOrderID(Container.DataItem)%><br />
        <asp:Repeater ID="rptLineItems" runat="server">
            <ItemTemplate>
                <%# Eval("SomeColumn1"); %>
                <%# Eval("SomeColumn2"); %>
                <%# Eval("SomeColumn3"); %>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAddLine" runat="server" CommandArgument=<%=cLastOrderID%> />
            </FooterTemplate>
        </asp:Repeater>
    </ItemTemplate
</asp:Repeater>

and on call behind

public int cLastOrderID = -1;

protected string GetOrderID(object oItem)
{
  cLastOrderID = (int)DataBinder.Eval(oItem, "OrderID");

  return cLastOrderID.ToString();
}

What I do here is that I call the GetOrderID to return the OrderID and I save it in a global value, and then later in the footer I use this global value. Hope this helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜