从 ASP.net 中的 XmlDataSource 计算 ListView 中的 ItemTemplate
我有以下代码。它按原样工作,但是...我的 RSS 提要中并不总是有偶数个项目因此,在表格的末尾,我可能在最后一行只有一个表格单元格。那么,有没有办法计算 ItemTemplates 和 AlternatingItemTemplate 的数量,这样如果它是奇数,我就可以添加另一个单元格 并关闭表格行?
<asp:XmlDataSource ID="SomeFeed" DataFile="TestSomeRSS.xml" XPath="rss/channel/item" runat="server"></asp:XmlDataSource>
<asp:ListView ID="SomeFeedScroller" DataSourceID="SomeFeed" ItemPlaceholderID="SomePlcID" runat="server">
<LayoutTemplate>
<table id="ListingsTable" cellpadding="0" cellspacing="0" align="center">
<asp:PlaceHolder ID="SomePlcID" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="vertical-align:top;">
<td class="bnotes" style="width:325px;padding:5px;">
<%# XPath("title")%><br />
<%# XPath("description")%><br />
</td>
</ItemTemplate>
<AlternatingItemTemplate>
<td class="bnotes" style="width:325px;padding:5px;">
<%# XPath("title")%><br />
<%# XPath("description")%><br />
</td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
预先感谢您的帮助。
I have the following code. It works as is, but... I am not always going to have an even number of items in the RSS feed So, at the end of the table, I might have only one table cell on the last row. So, is there a way to count the number of ItemTemplates and AlternatingItemTemplate, so if it is an odd number I would be able to add another cell <td> </td></tr>
and close the table row?
<asp:XmlDataSource ID="SomeFeed" DataFile="TestSomeRSS.xml" XPath="rss/channel/item" runat="server"></asp:XmlDataSource>
<asp:ListView ID="SomeFeedScroller" DataSourceID="SomeFeed" ItemPlaceholderID="SomePlcID" runat="server">
<LayoutTemplate>
<table id="ListingsTable" cellpadding="0" cellspacing="0" align="center">
<asp:PlaceHolder ID="SomePlcID" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="vertical-align:top;">
<td class="bnotes" style="width:325px;padding:5px;">
<%# XPath("title")%><br />
<%# XPath("description")%><br />
</td>
</ItemTemplate>
<AlternatingItemTemplate>
<td class="bnotes" style="width:325px;padding:5px;">
<%# XPath("title")%><br />
<%# XPath("description")%><br />
</td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您在问什么,但为什么不在 ItemTemplate 和 AlternatingItemTemplate 中放置一个完整的行,如下所示:
这样您就不必自己弄清楚 - 只需让控件自行呈现即可。
编辑添加
再次查看您发布的代码,看起来您可能一直在尝试一行交替的单元格样式。我认为您误解了 ItemTemplate 和 AlternatingItemTemplates 的意图;它们通常处理给定记录的字段(列)。
在这种情况下,您将在 ItemTemplate 中拥有第一个 RSS 提要项目,然后在 AlternateItemTemplate 中拥有第二个 RSS 提要项目(即另一行),然后在 ItemTemplate 中拥有第三个 RSS 提要项目,依此类推。
我希望这会有所帮助 - 如果我误解了您想要做的事情,请告诉我。
第二次编辑
根据评论中发布的示例布局,我认为 DataList 类 将是一个更好的选择,因为您可以轻松指定多个列(使用
RepeatColumns
属性)。像这样的事情:上面的内容没有经过测试,但总体思路是保持格式尽可能接近 ListView 中的格式。
另一种可能的方法可能类似于此线程中的中继器控件中的多列: 多列在中继器中。
DataList控件与ListView一样支持编辑、选择、更新等。 Repeater 控件则不然。
I'm not sure what you're asking, but why not just put a complete row in the ItemTemplate and AlternatingItemTemplate, like this:
That way you don't have to figure it out yourself - just let the control render itself.
EDITED TO ADD
Looking at your posted code again, it looks like you might have been attempting one row of alternating cell styles. I think you misunderstood the intent of the ItemTemplate and AlternatingItemTemplates; they usually deal with the fields (columns) of a given record.
In this case, you'd have the first RSS feed item in an ItemTemplate, then the second RSS feed item in an AlternateItemTemplate (i.e., another row), then the third RSS feed item in an ItemTemplate and so on.
I hope this helps - if I've misunderstood what you're trying to do let me know.
2nd Edit
Based on the example layout posted in the comments, I think the DataList Class would be a better option, as you can easily specify multiple columns (using the
RepeatColumns
property). Something like this:The above is not tested, but the general idea was to keep the formatting as close to what was in the ListView as possible.
Another possible approach might be something similar to this thread on having multiple columns in a Repeater control: Multiple columns in a repeater.
The DataList control supports editing, selecting, updating, etc like ListView. The Repeater control does not.