欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AspNetPager+GridView实现分页的实例代码

程序员文章站 2024-03-04 13:35:59
.框架是.net framework 4.0.一共为三个部分: 前台页面设计代码、前台页面程序代码、css样式.其中数据库连接操作用了db类(连接语句),sqlhelper...

.框架是.net framework 4.0
.一共为三个部分: 前台页面设计代码、前台页面程序代码、css样式
.其中数据库连接操作用了db类(连接语句),sqlhelper(微软的数据库操作类)
效果图:


前台页面设计代码

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="testwebsite.default" %>

<%@ register assembly="aspnetpager" namespace="wuqi.webdiyer" tagprefix="webdiyer" %>

<!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>
    <link href="styles/paging.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:gridview id="gridview1" runat="server" height="261px" width="737px"
            cellpadding="4" forecolor="#333333" gridlines="none">
            <alternatingrowstyle backcolor="white" />
            <editrowstyle backcolor="#2461bf" />
            <footerstyle backcolor="#507cd1" font-bold="true" forecolor="white" />
            <headerstyle horizontalalign="left" backcolor="#507cd1" font-bold="true"
                forecolor="white" />
            <pagerstyle backcolor="#2461bf" forecolor="white" horizontalalign="center" />
            <rowstyle backcolor="#eff3fb" />
            <selectedrowstyle backcolor="#d1ddf1" font-bold="true" forecolor="#333333" />
            <sortedascendingcellstyle backcolor="#f5f7fb" />
            <sortedascendingheaderstyle backcolor="#6d95e1" />
            <sorteddescendingcellstyle backcolor="#e9ebef" />
            <sorteddescendingheaderstyle backcolor="#4870be" />
        </asp:gridview>

    </div>
    <webdiyer:aspnetpager id="aspnetpager1" runat="server"
        onpagechanged="aspnetpager1_pagechanged" cssclass="anpager"
        currentpagebuttonclass="cpb" firstpagetext="首页" lastpagetext="尾页"
        nextpagetext="后页" prevpagetext="前页">
    </webdiyer:aspnetpager>
    </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 testwebsite.utilities;
using system.data;
using system.data.sqlclient;
using wuqi.webdiyer;

namespace testwebsite
{
    public partial class default : system.web.ui.page
    {
        protected void page_load(object sender, eventargs e)
        {
            if (!ispostback)
            {
                //调用绑定分页和gridview
                bindgridview();
            }
        }

        ////绑定分页和gridview方法
        private void bindgridview()
        {
            //查询语句
            string sequal = "select standardname as 标准名称, makeupitem as 补偿项目, unit as 单位,"
                + " cast(unitprice as decimal(18,2)) as 单价, cast(standrate as decimal(18,2)) as "
                + "成新率, type as 分类 from standard";
            //获取数据表格
            datatable dt =
                sqlhelper.executedataset(db.con, commandtype.text, sequal).tables[0];
            //初始化分页数据源实例
            pageddatasource pds = new pageddatasource();
            //设置总行数
            aspnetpager1.recordcount = dt.rows.count;
            //设置分页的数据源
            pds.datasource = dt.defaultview;
            //设置当前页
            pds.currentpageindex = aspnetpager1.currentpageindex - 1;
            //设置每页显示页数
            pds.pagesize = aspnetpager1.pagesize;
            //启用分页
            pds.allowpaging = true;
            //设置gridview的数据源为分页数据源
            gridview1.datasource = pds;
            //绑定gridview
            gridview1.databind();
        }

        protected void aspnetpager1_pagechanged(object sender, eventargs e)
        {
            //调用绑定分页和gridview
            bindgridview();
        }
    }
}


 css样式
复制代码 代码如下:

.anpager
{
    font: 11px arial, helvetica, sans-serif;
    padding:10px 20px 10px 0;
    margin: 0px;
}
.anpager a
{
    padding: 1px 6px;
    border: solid 1px #ddd;
    background: #fff;
    text-decoration: none;
    margin-right:2px
}
.anpager a:visited
{
    padding: 1px 6px;
    border: solid 1px #ddd;
    background: #fff;
    text-decoration: none;
}
.anpager .cpb
{
    padding: 1px 6px;
    font-weight: bold;
    font-size: 13px;
    border:none
}
.anpager a:hover
{
    color: #fff;
    background: #ffa501;
    border-color:#ffa501;
    text-decoration: none;
}

/* aspnetpager1属性设置: cssclass="anpager" currentpagebuttonclass="cpb"*/