2011-09-14 5 views
0

製品の詳細ページのメニューナビゲーションを構築しようとしていて、同じカテゴリーの製品のナビゲーション項目のみを表示したいとします。親値に基づいてリピーターのXmlDataSourceを条件付きでフィルターする方法

データは2つの異なるXMLファイルで管理されます。 現在の製品データを含むものと、ナビゲーション情報を含むも​​の。 どちらも "カテゴリ"を要素として含んでいます。

別のXmlDataSourceを使用し、XPath(「カテゴリ」)を使用してアクセス可能な親コンテナから現在の製品カテゴリをXPath属性に渡して値をフィルタリングしたいと考えています。

このフィルタリングされたデータソースをレンダリングのためにリピータに渡します。

<%-- get the current product XML --%> 
<asp:XmlDataSource ID="productDS" runat="server" XPath="/product" DataFile="~/App_Theme/project/products/poduct1.xml"/> 
<asp:DataList ID="product" DataSourceID="productDS" runat="server"> 
<ItemTemplate> 
    <%-- 
     get the navigation XML and filter the nodes to only show the navItems with the current product category 
    --%>  
    <asp:XmlDataSource ID="navItemsDS" runat="server" XPath="/navigation/navItems/navItem[category='<%# XPath("category") %>']" DataFile="~/App_Theme/project/productslist.xml"/> 

    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="navItemsDS" > 
     <HeaderTemplate> 
      <ul> 
     </HeaderTemplate> 
     <ItemTemplate> 
      <li><a href="productdetail.html?page=products&amp;id=<%# XPath("prodctid") %>"><%# XPath("producttitle") %></a></li> 
     </ItemTemplate> 
     <FooterTemplate> 
      </ul> 
     </FooterTemplate> 
    </asp:Repeater> 

    ... etc 

これは動作しません。

これを.NET 2.0でどうやって達成するか。

答えて

0

解決しました。

私はItemDataBoundのコードの後ろに次のようにして終了しました。

pspコード;後ろ

<%-- get the current product XML --%> 
<asp:XmlDataSource ID="productDS" runat="server" XPath="/product" DataFile="~/App_Theme/project/products/poduct1.xml"/> 
<asp:DataList ID="product" DataSourceID="productDS" runat="server"> 
<ItemTemplate> 
    <%-- 
     get the navigation XML and filter the nodes to only show the navItems with the current product category 
    --%>  
    <asp:XmlDataSource ID="navItemsDS" runat="server" XPath="/navigation/navItems/navItem" DataFile="~/App_Theme/project/productslist.xml"/> 

    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="navItemsDS" OnItemDataBound="navigation_ItemDataBound"> 
     <HeaderTemplate> 
      <ul> 
     </HeaderTemplate> 
     <ItemTemplate> 
      <li><a href="productdetail.html?page=products&amp;id=<%# XPath("prodctid") %>"><%# XPath("producttitle") %></a></li> 
     </ItemTemplate> 
     <FooterTemplate> 
      </ul> 
     </FooterTemplate> 
    </asp:Repeater> 

コード。

// hides navigation items that are not in the same category as this product. 
public void navigation_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    // get a navigator for the current navigation item xml 
    XPathNavigator nav = ((IXPathNavigable)e.Item.DataItem).CreateNavigator(); 
    // get a navigator for the product xml 
    XPathNavigator nav2 =((IXPathNavigable)((DataListItem)e.Item.Parent.Parent).DataItem).CreateNavigator(); 

    String itemCategory = nav.SelectSingleNode("category").Value; 
    String productCategory = nav2.SelectSingleNode("category").Value; 

    if (itemCategory != productCategory) 
    { 
     e.Item.Visible = false; 
    } 
} 

検索データリストのデータ項目、すなわちe.item.Parent.Parent.DataItemは最初に困難で動作するようにしました。

次に、実際のXMLノード値を取得することは、あいまいでした。

上記のコードは私にとってはうまくいきます。

0

個人的には、XMLをDataSetにロードし、Relationを定義して、親を子にマップします。

一般的なアプローチについては、hereを参照してください。

関連する問題