asp.net - 带有 Eval 的中继器的 SeparatorTemplate

标签 asp.net data-binding repeater eval

是否可以在中继器的 SeparatorTemplate 中使用 Eval 或类似语法?

我想在分隔符模板中显示最后一项的一些信息,如下所示:

<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>

希望它会产生这样的东西:
<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>

但是 SeparatorTemplate 似乎忽略了 Eval() 调用。我也尝试使用像这样的以前的语法: <%# DataBinder.Eval(Container.DataItem, "ArrivalAirport")%> 具有相同的结果。

是否可以在 SeparatorTemplate 中显示上一项的信息?如果没有,您能否建议另一种生成此代码的方法?

谢谢

最佳答案

试试这个:

在 WebForm 的类中添加一个(或两个)私有(private)变量,当您在项目级别执行数据绑定(bind)时,您可以使用它来增加/跟踪航类信息。

然后在 ItemDatabound 事件中,如果正在数据绑定(bind)的项目是 ListItemType.Seperator 类型,则可以执行简单的评估,并以这种方式显示/隐藏/修改您的分隔符代码。

您的 WebForm 页面在顶部看起来像这样:

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

    private int m_FlightStops;

    protected page_load
    {

        // Etc. Etc.

    }
}

然后,当您着手进行数据绑定(bind)时:
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;
       }
    }
 }

......或类似的东西。

关于asp.net - 带有 Eval 的中继器的 SeparatorTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/465889/

相关文章:

c# - 使用绑定(bind)禁用特定的 DataGrid 单元格

wpf - 如何在WPF中订购组

c# - 在 Repeater Asp.net 中查找标签控件

中继器的 JSF2 寻呼/寻呼机

jquery - asp.net mvc 3 的转发器类型控件

.net - 表单例份验证跨 Windows 身份验证

c# - Controller 上的 ActionFilterAttribute 与操作方法

asp.net - 如何在 ASP.NET 的服务器端获取输入值(输入文本)?

ASP.NET:如何正确重定向出现 404 错误的请求?

database - 如何处理数据库中的几十个标志