【问题标题】:Repeater's SeparatorTemplate with Eval带有 Eval 的中继器的 SeparatorTemplate
【发布时间】:2010-10-02 17:27:22
【问题描述】:

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

Id' 喜欢在分隔符模板中显示最后一项的一些信息,如下所示:

<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() 调用。我也尝试使用以前的语法,如下所示: 具有相同的结果。

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

谢谢

【问题讨论】:

    标签: asp.net data-binding repeater eval


    【解决方案1】:

    嘿,我会用一种方法来识别中继器中的最后一项,这样我就可以避免在那里生成分隔符:

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

    谢谢

    【讨论】:

      【解决方案2】:

      试试这个:

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

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

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

      public partial class ViewFlightInfo : System.Web.UI.Page
      {
      
          private int m_FlightStops;
      
          protected page_load
          {
      
              // Etc. Etc.
      
          }
      }
      

      然后,当您着手进行数据绑定时:

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

      ...或类似的东西。

      【讨论】:

      • 此解决方案依赖于 rFlightStops.Items.Count 具有完整的项目数,在我的测试中表明情况并非如此。它包含到目前为止已添加的项目数。所以它不会检测到最后一行。
      • rFlightStops 可能没有完整的项目数,但 rFlightStops.DataSource 应该有。
      猜你喜欢
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      相关资源
      最近更新 更多