问题描述
可以在Repeater的SeparatorTemplate中使用Eval或类似的语法吗?
is it possible to use Eval or similar syntax in the SeparatorTemplate of a Repeater?
Id'喜欢在分隔符模板中显示最后一个项目的某些信息,如这个:
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>
但是SeparatorTemplate似乎忽略了Eval()调用。我尝试使用以前的语法:<%#DataBinder.Eval(Container.DataItem,ArrivalAirport)%>具有相同的结果。
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.
是吗可能在SeparatorTemplate中显示上一个项目的信息?如果没有,你可以建议另一种方法来生成这个代码吗?
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?
谢谢
推荐答案
尝试这样:
在您的WebForm类中添加一个私有变量(或两个),您可以使用它来增加/跟踪哪些航班信息而您在项目级别执行数据绑定。
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.
然后在ItemDatabound事件中,如果数据绑定的项目是ListItemType.Seperator类型,则可以执行简单的评估,显示/隐藏/修改您的分隔符代码。
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.
您的WebForm页面将在顶部看起来像这样:
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.
这篇关于Repeater的SeparatorTemplate与Eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!