Repeater绑定Xml数据源或文件
1、要引用的命名空间
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
2、Repeater控件绑定XmlDataSource数据源,无分页
1)前台TestRepeater.aspx文件:
<asp:Repeater ID="xml1" runat="server" DataSourceID="XmlDataSource1">
<ItemTemplate>
<ul>
<li style="color: Blue;">《<%# Server.HtmlEncode(XPath("name").ToString()) %>》</li>
<li><%# Server.HtmlEncode(XPath("mark").ToString())%></li>
<li><b>数量:</b><%# XPath("quantity")%> <b>价格:</b><%# XPath("price")%> <b>作者:</b><%# XPath("author")%></li>
</ul>
<hr />
</ItemTemplate>
</asp:Repeater>
<asp:XmlDataSource runat="server" DataFile="~/data/test.xml"></asp:XmlDataSource>
2)后台TestRepeater.aspx.cs文件
如没有特殊需求,后台无需任何代码即可显示数据。
3、Repeater控件通过DataTable(DataView)直接绑定xml文件,有分页
1)前台TestRepeater.aspx文件:
<asp:Repeater ID="xml1" runat="server">
<ItemTemplate>
<ul>
<li style="color: Blue;"><a style="color: Red;" href='?id=<%# Eval("id") %>'>删除</a>《<%# Server.HtmlEncode(Eval("name").ToString()) %>》</li>
<li><b>数量:</b><%# Eval("quantity")%> <b>价格:</b><%# Eval("price")%> <b>作者:</b><%# Eval("author")%></li>
</ul>
<hr />
</ItemTemplate>
</asp:Repeater>
<ul>
<li>
<asp:HyperLink ID="hyHome" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="hyPrev" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="hyNext" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="hyEnd" runat="server" Text="尾页"></asp:HyperLink>
共 <asp:Literal ID="recTotal" runat="server"></asp:Literal> 项,<asp:Literal ID="pageCount" runat="server"></asp:Literal> 页,当前第 <asp:Literal ID="pageIndex" runat="server"></asp:Literal> 页
</li>
</ul>
2)后台TestRepeater.aspx.cs文件:
protected void Page_Load(object sender, EventArgs e)
{
string file = Server.MapPath("/data/test.xml");
DataSet ds = new DataSet();
ds.ReadXml(file);
DataTable dt = ds.Tables[0];
//执行删除操作
int id = Convert.ToInt32(Request.QueryString["id"]);
if (id > 0)
{
dt.Rows[id-1].Delete();
dt.AcceptChanges();
ds.WriteXml(file);
}
//执行删除操作
DataView dv = dt.DefaultView;
dv.Sort = "date desc";
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.PageSize = 10;
int pageIndex = Convert.ToInt32(Request.QueryString["page"]);
if (pageIndex <= 1) pageIndex = 1;
pds.CurrentPageIndex = pageIndex - 1;
this.xml1.DataSource = pds;
this.xml1.DataBind();
this.recTotal.Text = pds.DataSourceCount.ToString();
this.pageCount.Text = pds.PageCount.ToString();
this.pageIndex.Text = pageIndex.ToString();
if (pageIndex == 1)
this.hyHome.NavigateUrl = string.Empty;
else
this.hyHome.NavigateUrl = "?page=1";
if (pageIndex > 1)
this.hyPrev.NavigateUrl = "?page=" + (pageIndex - 1) + "";
else
this.hyPrev.NavigateUrl = string.Empty;
if (pageIndex < pds.PageCount)
this.hyNext.NavigateUrl = "?page=" + (pageIndex + 1) + "";
else
this.hyNext.NavigateUrl = string.Empty;
if (pageIndex >= pds.PageCount)
this.hyEnd.NavigateUrl = string.Empty;
else
this.hyEnd.NavigateUrl = "?page=" + (pds.PageCount) + "";
}
4、xml文件格式:
<?xml version="1.0" encoding="utf-8"?>
<books>
<book>
<id>1</id>
<name>神州侠侣</name>
<quantity>50</quantity>
<price>45.55</price>
<author>tiger</author>
</book>
</books>
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
2、Repeater控件绑定XmlDataSource数据源,无分页
1)前台TestRepeater.aspx文件:
<asp:Repeater ID="xml1" runat="server" DataSourceID="XmlDataSource1">
<ItemTemplate>
<ul>
<li style="color: Blue;">《<%# Server.HtmlEncode(XPath("name").ToString()) %>》</li>
<li><%# Server.HtmlEncode(XPath("mark").ToString())%></li>
<li><b>数量:</b><%# XPath("quantity")%> <b>价格:</b><%# XPath("price")%> <b>作者:</b><%# XPath("author")%></li>
</ul>
<hr />
</ItemTemplate>
</asp:Repeater>
<asp:XmlDataSource runat="server" DataFile="~/data/test.xml"></asp:XmlDataSource>
2)后台TestRepeater.aspx.cs文件
如没有特殊需求,后台无需任何代码即可显示数据。
3、Repeater控件通过DataTable(DataView)直接绑定xml文件,有分页
1)前台TestRepeater.aspx文件:
<asp:Repeater ID="xml1" runat="server">
<ItemTemplate>
<ul>
<li style="color: Blue;"><a style="color: Red;" href='?id=<%# Eval("id") %>'>删除</a>《<%# Server.HtmlEncode(Eval("name").ToString()) %>》</li>
<li><b>数量:</b><%# Eval("quantity")%> <b>价格:</b><%# Eval("price")%> <b>作者:</b><%# Eval("author")%></li>
</ul>
<hr />
</ItemTemplate>
</asp:Repeater>
<ul>
<li>
<asp:HyperLink ID="hyHome" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="hyPrev" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="hyNext" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="hyEnd" runat="server" Text="尾页"></asp:HyperLink>
共 <asp:Literal ID="recTotal" runat="server"></asp:Literal> 项,<asp:Literal ID="pageCount" runat="server"></asp:Literal> 页,当前第 <asp:Literal ID="pageIndex" runat="server"></asp:Literal> 页
</li>
</ul>
2)后台TestRepeater.aspx.cs文件:
protected void Page_Load(object sender, EventArgs e)
{
string file = Server.MapPath("/data/test.xml");
DataSet ds = new DataSet();
ds.ReadXml(file);
DataTable dt = ds.Tables[0];
//执行删除操作
int id = Convert.ToInt32(Request.QueryString["id"]);
if (id > 0)
{
dt.Rows[id-1].Delete();
dt.AcceptChanges();
ds.WriteXml(file);
}
//执行删除操作
DataView dv = dt.DefaultView;
dv.Sort = "date desc";
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.PageSize = 10;
int pageIndex = Convert.ToInt32(Request.QueryString["page"]);
if (pageIndex <= 1) pageIndex = 1;
pds.CurrentPageIndex = pageIndex - 1;
this.xml1.DataSource = pds;
this.xml1.DataBind();
this.recTotal.Text = pds.DataSourceCount.ToString();
this.pageCount.Text = pds.PageCount.ToString();
this.pageIndex.Text = pageIndex.ToString();
if (pageIndex == 1)
this.hyHome.NavigateUrl = string.Empty;
else
this.hyHome.NavigateUrl = "?page=1";
if (pageIndex > 1)
this.hyPrev.NavigateUrl = "?page=" + (pageIndex - 1) + "";
else
this.hyPrev.NavigateUrl = string.Empty;
if (pageIndex < pds.PageCount)
this.hyNext.NavigateUrl = "?page=" + (pageIndex + 1) + "";
else
this.hyNext.NavigateUrl = string.Empty;
if (pageIndex >= pds.PageCount)
this.hyEnd.NavigateUrl = string.Empty;
else
this.hyEnd.NavigateUrl = "?page=" + (pds.PageCount) + "";
}
4、xml文件格式:
<?xml version="1.0" encoding="utf-8"?>
<books>
<book>
<id>1</id>
<name>神州侠侣</name>
<quantity>50</quantity>
<price>45.55</price>
<author>tiger</author>
</book>
</books>