Repeater绑定Xml数据源或文件

13 篇文章 0 订阅
3 篇文章 0 订阅
 

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")%>&nbsp;&nbsp;<b>价格:</b><%# XPath("price")%>&nbsp;&nbsp;<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")%>&nbsp;&nbsp;<b>价格:</b><%# Eval("price")%>&nbsp;&nbsp;<b>作者:</b><%# Eval("author")%></li>
                </ul>
                <hr />
        </ItemTemplate>
</asp:Repeater>
<ul>
        <li>
                <asp:HyperLink ID="hyHome" runat="server" Text="首页"></asp:HyperLink>&nbsp;&nbsp;
                <asp:HyperLink ID="hyPrev" runat="server" Text="上一页"></asp:HyperLink>&nbsp;&nbsp;
                <asp:HyperLink ID="hyNext" runat="server" Text="下一页"></asp:HyperLink>&nbsp;&nbsp;
                <asp:HyperLink ID="hyEnd" runat="server" Text="尾页"></asp:HyperLink>&nbsp;&nbsp;
                共 <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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值