ASP.NET 2.0 分页技术之使用PagedDataSource篇

时间:2021-09-02 08:11:54

大家好,好久没有更新我的博客了,特地整理了关于分页技术的知识点,堪称超简单实用的分页.
--------------------------------------------------------------------------------

前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <TABLE borderColor=#e5e3e3 cellSpacing=0 cellPadding=3 width=790 align=center border=1>
              <TR bgColor=#fff9ec>
                <TH align="center" width=200 bgColor=#fff9ec>名称</TH>
                <TH align="center" width=200 bgColor=#fff9ec>类别</TH>
                <TH align="center" width=200 bgColor=#fff9ec>最小值</TH>
                <TH align="center" width=200 bgColor=#fff9ec>最大值</TH>
              </TR>
        </HeaderTemplate>
        <ItemTemplate>
            <TR>
                <TD align="center" width=200><%#Eval("job_id")%></TD>
                <TD align="center" width=200><%#Eval("job_desc")%></TD>
                <TD align="center" width=200><%#Eval("min_lvl")%></TD>
                <TD align="center" width=200><%#Eval("max_lvl")%></TD>
            </TR>
        </ItemTemplate>
        <FooterTemplate>
            </TABLE>
        </FooterTemplate>
        </asp:Repeater>
    </div>
    <p></p>
    <div align="center">
                                        当前页<asp:Label ID="nowpage" runat="server" Font-Bold="True"></asp:Label>/<asp:Label ID="allpages"
                                        runat="server" Font-Bold="True" ForeColor="#C00000"></asp:Label>  
                                        每页 <asp:Label ID="onepage" runat="server" Font-Bold="True"></asp:Label> 条  共 <asp:Label ID="allpage"
                                        runat="server" Font-Bold="True"></asp:Label> 页<asp:HyperLink ID="firstpage"
                                            runat="server">第一页</asp:HyperLink> 
                                        <asp:HyperLink ID="prepage" runat="server">上一页</asp:HyperLink>  
                                        <asp:HyperLink ID="nextpage" runat="server">下一页</asp:HyperLink>  <asp:HyperLink
                                            ID="endpage" runat="server">末一页</asp:HyperLink>
        共有 <asp:Label
                                        ID="allmsg" runat="server" Font-Bold="True"></asp:Label> 条留言
                                        </div>
                                        <br />
                                        <div align="center"><b>PowerBy ABEN</b> <a href="http://blog.sina.com.cn/whaben" target="_blank">http://blog.sina.com.cn/whaben</a>
</div>
    </form>
</body>
</html>

后台事件代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//------导包
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int CurPage;
        if (Request.QueryString["Page"] != null)
            CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        else
            CurPage = 1;
 //连接数据库及将数据封装到一个数据集中
        SqlConnection con = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=sa;");
        SqlDataAdapter adapter = new SqlDataAdapter("select * from jobs", con);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "jobs");

        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = ds.Tables["jobs"].DefaultView;
        ps.AllowPaging = true;
        //每个页面显示的条数
        ps.PageSize = 5;
        onepage.Text = ps.PageSize.ToString();
        //求数据的总数
        allmsg.Text = ps.DataSourceCount.ToString();
        ps.CurrentPageIndex = CurPage - 1;
        //求总页
        allpage.Text = ps.PageCount.ToString();
        allpages.Text = ps.PageCount.ToString();
        nowpage.Text = CurPage.ToString();
        //将数据源与控件绑定
        Repeater1.DataSource = ps;
        Repeater1.DataBind();

        //上一页
        if (!ps.IsFirstPage)
        {
            firstpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
            prepage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
        }
        //下一页
        if (!ps.IsLastPage)
        {
            nextpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
            endpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(ps.PageCount);
        }
    }
}

如上述代码有什么问题请留言或联系我!