문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top