带 Eval 的中继器 SeparatorTemplate

发布于 2024-07-12 09:37:18 字数 1488 浏览 8 评论 0原文

是否可以在 Repeater 的 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 中显示前一项的信息? 如果没有,您能建议一种生成此代码的替代方法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦旅人picnic 2024-07-19 09:37:18

尝试这样做:

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

...或类似的效果。

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.

温柔戏命师 2024-07-19 09:37:18

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

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

谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文