views:

1457

answers:

3

Hi, is it possible to use Eval or similar syntax in the SeparatorTemplate of a Repeater?

Id' like to display some info of the last item in the separator template like this:

<table>
    <asp:Repeater>
        <ItemTemplate>
            <tr>
                <td><%# Eval("DepartureDateTime") %></td>
                <td><%# Eval("ArrivalDateTime") %></td>
            </tr>
        </ItemTemplate>
        <SeparatorTemplate>
            <tr>
                <td colspan="2">Change planes in <%# Eval("ArrivalAirport") %></td>
            </tr>
        </SeparatorTemplate>
    <asp:Repeater>
<table>

Hopping that it'll generate something like this:

<table>
    <asp:Repeater>
            <tr>
                <td>2009/01/24 10:32:00</td>
                <td>2009/01/25 13:22:00</td>
            </tr>
            <tr>
                <td colspan="2">Change planes in London International Airport</td>
            </tr>
            <tr>
                <td>2009/01/25 17:10:00</td>
                <td>2009/01/25 22:42:00</td>
            </tr>
    <asp:Repeater>
<table>

But the SeparatorTemplate seems to be ignoring the Eval() call. I tried using also the previous syntax like this: <%# DataBinder.Eval(Container.DataItem, "ArrivalAirport")%> with the same results.

Is it possible to display information of the previous item in a SeparatorTemplate? If not, can you suggest an alternative way to generate this code?

Thanks

A: 

Hey, I'll settle with a way to identify the last item in the repeater so that I can avoid generating the separator there:

<table>
    <asp:Repeater>
        <ItemTemplate>
            <tr>
                <td><%# Eval("DepartureDateTime") %></td>
                <td><%# Eval("ArrivalDateTime") %></td>
            </tr>
            <% if (<<<isn't the last item>>) { %>
            <tr>
                <td colspan="2">Change planes in <%# Eval("ArrivalAirport") %></td>
            </tr>
            <% } %>
        </ItemTemplate>
    <asp:Repeater>
<table>

Thanks

A: 

Just tried this but still doesn't work:

<table>
    <asp:Repeater ID="rFlightStops" runat="server"
        OnItemDataBound="rFlightStops_ItemDataBound">
        <ItemTemplate>
            <tr>
                <td><%# Eval("DepartureDateTime") %></td>
                <td><%# Eval("ArrivalDateTime") %></td>
            </tr>
            <asp:PlaceHolder ID="phChangePlanes" runat="server">
            <tr>
                <td colspan="2">Change planes in <%# Eval("ArrivalAirport") %></td>
            </tr>
            </asp:PlaceHolder>
        </ItemTemplate>
    <asp:Repeater>
<table>

And added the following to the Code Behind:

    protected void rFlightStops_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Repeater rFlightStops = (Repeater)sender;
        if (e.Item.ItemType == ListItemType.Item
            || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (e.Item.ItemIndex == rFlightStops.Items.Count - 1)
            {
                PlaceHolder phChangePlanes = 
                    (PlaceHolder)e.Item.FindControl("phChangePlanes");
                phChangePlanes.Visible = false;
            }
        }
    }

But rFlightStops.Items.Count it's always equal to e.Item.ItemIndex there so the if never executes.

Any help?

Thanks

+1  A: 

Try this:

Add a private variable (or two) in the class of your WebForm that you can use to increment/track what the flight information is while you are performing your databinding at the item level.

Then in the ItemDatabound event, you can perform a simple evaluation if the item being databound is the ListItemType.Seperator type and display/hide/modify your seperator code that way.

Your WebForm page would look something like this at the top:

public partial class ViewFlightInfo : System.Web.UI.Page
{

    private int m_FlightStops;

    protected page_load
    {

        // Etc. Etc.

    }
}

Then when you get down to your data binding:

protected void rFlightStops_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Repeater rFlightStops = (Repeater)sender;

    if (e.Item.ItemType == ListItemType.Header)
    {
        // Initialize your FlightStops in the event a new data binding occurs later. 
           m_FlightStops = 0;
    }

    if (e.Item.ItemType == ListItemType.Item
        || e.Item.ItemType == ListItemType.AlternatingItem)
    {
         // Bind your Departure and Arrival Time
         m_FlightStops++;
     }

    if (e.Item.ItemType == ListItemType.Seperator)
    {
       if (m_FlightStops == rFlightStops.Items.Count - 1)
       {
           PlaceHolder phChangePlanes = 
                    (PlaceHolder)e.Item.FindControl("phChangePlanes");
           phChangePlanes.Visible = false;
       }
    }
 }

...or something to this effect.

Dillie-O
This solution relies on rFlightStops.Items.Count having the complete number of items, which in my tests show that's not the case. It contains the number of items that have been added until now. So it doesn't detect the last row.