操作datatable进行分页,绑定repeqter或者gridView

今天上午研究一个多小时了终于出来了。

前台页面

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="tz_table" border="0" cellpadding="0" cellspacing="0" width="100%">
        <thead>
            <tr>
                <td>标题</td>
                <td>创建人</td>
                <td>到期日期</td>
                <td>未读</td>
            </tr>
        </thead>
        <tbody>
            <asp:Repeater ID="rptNotice" runat="server">
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:LinkButton ID="LinkButton1" Text='<%# Eval("Title") %>' runat="server"></asp:LinkButton>
                        </td>
                        <td>
                             <asp:Label ID="Label1" runat="server" Text='<%# Eval("Expires") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Author") %>'></asp:Label>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </tbody>
    </table>
       <%-- <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            AutoGenerateColumns="False" PageSize="5">
         <Columns>
            <asp:TemplateField HeaderText="标题">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" Text='<%# Eval("Title") %>' runat="server"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="时间">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Expires") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="提问人">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("Author") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        </asp:GridView>--%>
        <br />
        <asp:Button ID="Button1" runat="server" Text="上一页" οnclick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="下一页" />
    </div>
    </form>
</body>
</html>

 

 

然后后台绑定

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace LswTest
{
    public partial class GrideView : System.Web.UI.Page
    {
        static int pageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            //页面首次加载
            if (!IsPostBack)
            {
                rptNotice.DataSource = GetPagedTable(getTable(), pageIndex, 5);
                rptNotice.DataBind();
            }
        }
        //上一页
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            else
            {
                pageIndex -= 1;
            }
            rptNotice.DataSource = GetPagedTable(getTable(), pageIndex, 5);
            rptNotice.DataBind();
        }
        //下一页
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (pageIndex > 3)
            {
                pageIndex = 3;
            }
            else
            {
                pageIndex += 1;
              
            }
            rptNotice.DataSource = GetPagedTable(getTable(), pageIndex, 5);
            rptNotice.DataBind();
        }
        //获取加载时的table
        public DataTable getTable() {
            DataTable dt = new DataTable();
            //dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Title", typeof(string));//标题
            dt.Columns.Add("Author", typeof(string));//创建者
            dt.Columns.Add("Expires", typeof(DateTime));//到期日期

            DataRow dr;
            for (int i = 0; i < 15; i++)
            {
                dr = dt.NewRow();
                dr["Title"] = "第" + i + "条数据";
                dr["Author"] = "user00" + i;
                dr["Expires"] = DateTime.Now.ToString();
                dt.Rows.Add(dr);
            }
            return dt;
        }
        //操作上一页下一页的table
        public DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
        {
            if (PageIndex == 0)
                return dt;
            DataTable newdt = dt.Copy();
            newdt.Clear();

            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
                return newdt;

            if (rowend > dt.Rows.Count)
                rowend = dt.Rows.Count;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }

            return newdt;
        }
    }
}

后台页面的dataTable中数据是我自己模拟的数据,也可以从数据库中查询出来放到dataTable中,然后再对datatable进行操作!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值