2011-09-03 25 views
0

我有以下代碼。它的工作原理是這樣,但是...我並不總是會在RSS源中有偶數個項目所以,在表的最後,我最後一行可能只有一個表格單元格。那麼,有沒有辦法計算ItemTemplates和AlternatingItemTemplate的數量,所以如果它是一個奇數,我可以添加另一個單元格<td>&nbsp;</td></tr>並關閉表格行?從ASP.net中的XmlDataSource在ListView中計數ItemTemplate

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

在此先感謝您的幫助。

回答

1

我不知道你問什麼,但爲什麼不乾脆把整排在ItemTemplate和AlternatingItemTemplate,像這樣:

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

這樣,你不必弄清楚你自己 - 讓控制呈現自己。

編輯補充

在您發佈的代碼看一次,它看起來像你可能一直在試圖交替的單元格樣式的一行。我認爲你誤解了ItemTemplate和AlternatingItemTemplates的意圖;他們通常處理給定記錄的字段(列)。

在這種情況下,您需要在ItemTemplate中創建第一個RSS源項目,然後在AlternateItemTemplate(即另一行)中創建第二個RSS源項目,然後在ItemTemplate中創建第三個RSS源項目,依此類推。

我希望這會有所幫助 - 如果我誤解了你想要做的事情,請告訴我。

第二編輯

根據張貼在評論示例佈局中,我覺得DataList Class會是一個更好的選擇,因爲你可以很容易地指定多個列(使用RepeatColumns屬性)。事情是這樣的:

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

以上沒有進行測試,但總的想法是保留原來的格式緊挨什麼是在ListView越好。

另一種可能的方法可能與此線程在Repeater控件中有多列相似:Multiple columns in a repeater

DataList控件支持像ListView一樣的編輯,選擇,更新等。 Repeater控制不。

+0

也許我完全接近這個完全錯誤的。我正在嘗試的是使用RSS提要中的數據構建表格。該表是兩列寬。每個單元格將包含每個項目所需的所有數據。 – Soren

+0

如果我嘗試: <%#的XPath ( 「標題」)%>
<%#的XPath( 「描述」)%>
<%#的XPath( 「標題」)%>
<%#的XPath( 「描述」)%>
然後我會在兩個單元格中獲得相同的內容。 – Soren

+0

@Soren - 你將兩個單元放在同一個Item Template中。你是否試圖並排顯示兩個單元格?另外,你可以用XML的樣例更新你的問題嗎? – Tim

相關問題