我正在为自行车商店创建一个网站,其中应该有两排自行车(图像和文字)并排在页面上。我试图在gridview中有2行,其中数据是从XML文件填充的。我遇到的问题是每行包含与旁边的数据相同的数据。有谁知道我怎么能解决这个问题 - 即。每行都有新数据?我从来没有使用过gridview或XML文件,所以如果这很明显就道歉了!
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="XmlDataSource1" AllowSorting="True" HorizontalAlign="Center"
Width="800px" AllowPaging="True" AllowCustomPaging="True" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="ProductName"
runat="server"
Text='<%# XPath("ProductName") %>'></asp:Label>
<asp:Label ID="ProductDescription"
runat="server"
Text='<%# XPath("Description") %>'></asp:Label>
<asp:Label ID="Cost"
runat="server"
Text='<%# XPath("Cost") %>'></asp:Label>
<asp:Image ID="Image1"
runat="server" Height="200px"
ImageUrl='<%# XPath("Image") %>'
Width="200px" Align="left"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="ProductName"
runat="server"
Text='<%# XPath("ProductName") %>'></asp:Label>
<asp:Label ID="ProductDescription"
runat="server"
Text='<%# XPath("Description") %>'></asp:Label>
<asp:Label ID="Cost"
runat="server"
Text='<%# XPath("Cost") %>'></asp:Label>
<asp:Image ID="Image1"
runat="server" Height="200px"
ImageUrl='<%# XPath("Image") %>'
Width="200px" Align="left"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Products.xml"
XPath="Products/Bikes/MaleBikes/RoadBikes"></asp:XmlDataSource>
一些XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<Products>
<Bikes>
<MaleBikes>
< RoadBikes>
<ProductId>1</ProductId>
<ProductName>Ridley Fenix C20 1409AM Ultegra 11 Speed 2014</ProductName>
<Description>With the Fenix C20 you are now entering the top-end bikes in this range. Kitted out with Shimano Ultegra 11 speed components - giving 22 gears, Fulcrum Racing Quattro wheels, 4ZA Stratos parts, this bike is designed for faster, harder riding." </Description>
<Cost>€ 2950.00</Cost>
<Image>~/Images/MensBikes/2014 Ridley Fenix C20 Ultegra 11 Speed Carbon Road Bike.jpg</Image>
<CagegoryId>1</CagegoryId>
</RoadBikes>
<RoadBikes>
<ProductId>2</ProductId>
<ProductName>LOOK 675 Ultegra CT Road Bike 2013</ProductName>
<Description>
Look’s new all-rounder is as distinctive to ride as it is to look at, and it’s definitely more about power and precision than it is plushy pootling.
</Description>
<Cost>€ 3000</Cost>
<Image>~/Images/MensBikes/LOOK 675 Ultegra CT Road Bike 2013.jpg</Image>
<CagegoryId>1</CagegoryId>
</RoadBikes>
<RoadBikes>
<ProductId>3</ProductId>
<ProductName>Cube EPO (Electric) Bike 2012</ProductName>
<Description>Sporty pedelec with a difference! Design for purists who want to strike with "understatement". The tapered rigid fork and the lightweight, triple butted tubes make the frame stiff and fast. The disc brakes ensure the required braking performance for the higher average speed.</Description>
<Cost>€ 2880</Cost>
<Image>~/Images/MensBikes/Cube EPO (Electric) Bike 2012.jpg</Image>
<CategoryId>1</CategoryId>
</RoadBikes>
答案 0 :(得分:0)
我对你的建议是不要让你自己处理XML
复杂性。
如果您的XML
在数据库中,请阅读以下主题:How to loop and parse xml parameter in sql server stored procedure。
如果您的XML
在您的计划中,请阅读以下主题:load xelement into a datatable
选择数据后,您可以使用Eval
代替XPath
,我希望您的问题会被删除