2

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?

Community
  • 1
  • 1
Bryan
  • 8,748
  • 7
  • 41
  • 62

1 Answers1

2

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.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Thanks. Ended up doing something very similar to this with a global variable. Just felt a bit hacky. – Bryan Oct 05 '11 at 18:02
  • @Bryan its not hacky, just see how this control works in depth, and its a normal to save the last variable, and then use it. Nothing hacky. – Aristos Oct 05 '11 at 19:35