0

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>&nbsp;</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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Soren
  • 797
  • 5
  • 15
  • 32

1 Answers1

1

I'm not sure what you're asking, but why not just put a complete row in the ItemTemplate and AlternatingItemTemplate, like this:

<ItemTemplate>
    <tr style="vertical-align:top;">
        <td class="bnotes" style="width:325px;padding:5px;">         
            <%# XPath("title")%><br />         
            <%# XPath("description")%><br />     
        </td>
    </tr> 
</ItemTemplate>  
<AlternatingItemTemplate>
    <tr style="vertical-align:top;">     
        <td class="bnotes" style="width:325px;padding:5px;">         
            <%# XPath("title")%><br />         
            <%# XPath("description")%><br />     
        </td>     
    </tr> 
</AlternatingItemTemplate>

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:

<asp:XmlDataSource ID="SomeFeed" DataFile="TestSomeRSS.xml" XPath="rss/channel/item" runat="server">
</asp:XmlDataSource>

<asp:DataList ID="SomeFeedScroller" DataSourceID="SomeFeed" 
              RepeatColumns="2" RepeatDirection="Horizontal" 
              RepeatLayout="Table" runat="server">
    <ItemStyle CssClass="bnotes" Vertical-Align="top" Width="325px" />
    <AlternatingItemStyle CssClass="bnotes" vertical-Align="top" Width="325px" />
    <ItemTemplate>
        <%# XPath("title")%><br />
        <%# XPath("description")%>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <%# XPath("title")%><br />
        <%# XPath("description")%>
    </AlternatingItemTemplate>
</asp:DataList>

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.

Tim
  • 28,212
  • 8
  • 63
  • 76
  • Maybe I am completely approaching this completely wrong. What I am trying to do is build a table with data from a RSS feed. The table is two columns wide. Each cell will contain all the data I need from each item. – Soren Sep 03 '11 at 02:33
  • If I try: <%# XPath("title")%>
    <%# XPath("description")%>
    <%# XPath("title")%>
    <%# XPath("description")%>
    Then I'll get the same content in both cells.
    – Soren Sep 03 '11 at 02:36
  • @Soren - you're putting both cells in the same Item Template. Are you trying to display two cells side by side? Also, can you update your question with a sample of the XML? – Tim Sep 03 '11 at 05:24
  • Its just a standard RSS XML feed. What I am trying to do is display the the content of `item` in the first cell, then the next content of `item` in the next cell. The third would be on the next row, and so on. It's actually a product feed from an RSS feed. The results would be similar to http://www.triumphconnection.com/Books.asp which I did in classic ASP. – Soren Sep 04 '11 at 16:02
  • @Soren - See my edit above. The example you linked to was great as it made it clear what you were trying to accomplish. – Tim Sep 04 '11 at 22:31
  • Thank you very much for your help! You last example did the trick. You helped me learn something huge. – Soren Sep 06 '11 at 18:23