如何将数据从xml数据源读取到网页的标准控件中 [英] how to read data from xml datasource into a standard controls of a web page
本文介绍了如何将数据从xml数据源读取到网页的标准控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在
下创建了一个xml数据源
i have created a xml datasource as under
<Item>
<id>Motorola Blackflip</id>
<Description>Motorola Backflip sports a 3.1-inch HVGA capacitive touchscreen</Description>
<feature>Touchscreen,Wi-Fi,Bluetooth,5 MP Camera,Android OS</feature>
<price>Rs.18000</price>
<image>~/Image/Motorola-Backflip.jpeg</image>
</Item>
并与数据列表绑定,如下所示
and binded with datalist as under
<asp:DataList ID="DataList1" runat="server" Height="498px" Width="566px"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Vertical" RepeatColumns="2"
onitemcommand="DataList1_ItemCommand">
<AlternatingItemStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#F7F7DE" />
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%#Eval("image")%>'
Height="150" Width="150" CommandName="product" CommandArgument='<%#Eval("image")%>'/><br />
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%#Eval("Description") %>'
CommandName="id" CommandArgument='<%#Eval("id") %>'></asp:LinkButton>
</ItemTemplate>
现在使用冒泡事件,我正在读取数据并重定向到其他页面,如下所示
now using bubbled event i am reading data and redirecting to some other page as shown bellow
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "product")
{
Response.Redirect("ViewProduct.aspx?img=" + e.CommandArgument.ToString());
}
else
{
Response.Redirect("Description.aspx?de=" + e.CommandArgument.ToString());
}
}
}
现在,在重定向页面中,我希望将剩余的xml数据保存到某些控件中,例如标签,图像等.
请帮助获取剩余数据.
now in the redirected page i want remaining xml data into some controls like label,image etc.
please help to get remaining data.
推荐答案
在空闲时间为您编写的代码,请看一下
Code written just for you in free time, Take a look
XmlDocument doc = new XmlDocument();
doc.Load("");
XmlNodeList nodeList = doc.GetElementsByTagName("Item");
foreach (XmlNode node in nodeList)
{
//Check with respected id value you have got
if (node["id"].Value == "Motorola Blackflip")
{
Panel panel = new Panel();
foreach (XmlElement element in node)
{
if (element.Name != "image")
{
Label lbl = new Label();
lbl.Text = String.Format("<b>{0}</b>:{1}", element.Name, element.Value); ;
panel.Controls.Add(lbl);
}
else
{
Image img = new Image();
img.ImageUrl = element.Value;
panel.Controls.Add(img);
}
}
Page.Controls.Add(panel);
}
}
如果有帮助,请 投票 和 接受答案 .
Please vote and Accept Answer if it Helped.
这篇关于如何将数据从xml数据源读取到网页的标准控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文