第一步首先导入aspnetpager控件,然后再把他从工具箱中拖出,代码如下:

 <webdiyer:AspNetPager ID="aspnetpager1" runat="server" CustomInfoTextAlign="Left"
            FirstPageText="首页 " LastPageText ="末页" NextPageText=" 下一页" NumericButtonCount ="5"
            PageIndexBoxType="TextBox" PrevPageText="上一页 " ShowPageIndexBox ="Never" PageSize="20"
            AlwaysShow="True" CssClass="paginator" CurrentPageButtonClass="cpb" Wrap="True"
            BorderStyle="None" Width="100%" UrlPaging="True" CustomInfoClass="info" CustomInfoHTML =""
            CustomInfoSectionWidth="0px" OnPageChanged="aspnetpager1_PageChanged">
        </webdiyer: AspNetPager>
并且在页面的上部要引用下面代码:
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
之后要在引用里添加aspnetpager.dll引用
 
然后在后台page_load里添加以下分页代码:
                    Page_gkxx sp = new Page_gkxx();
 
                    aspnetpager1.EnableUrlRewriting = true ;
                aspnetpager1.UrlRewritePattern = "/zuowen/list-" + ParentTypeId + "-" + TypeId + "-" + GradeId + "-{0}.html" ;
                sp.InitBindData(Repeater1, aspnetpager1, "zuowen" , "id" , Sear(), GetFields());
其中InitBindData方法代码为:
public void InitBindData( Control c, Wuqi.Webdiyer. AspNetPager aspnet, string tbname, string tbcolumn, stringsqlwhere,string sfields)
        {
            aspnet.RecordCount = GetRows(tbname, sqlwhere);
            if (aspnet.RecordCount > 0)
            {
                string contype = c.GetType().ToString();
 
                DataSet ds = GetList(tbname, tbcolumn,sfields,aspnet.PageSize, aspnet.CurrentPageIndex, sqlwhere);
 
 
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (contype.IndexOf("GridView" ) != -1)
                    {
                        GridView gv = (GridView )c;
                        gv.DataSource = ds;
                        gv.DataBind();
                    }
                    else if (contype.IndexOf( "Repeater") != -1)
                    {
                        Repeater rep = (Repeater )c;
                        rep.DataSource = ds;
                        rep.DataBind();
                    }
                    else if (contype.IndexOf( "DataList") != -1)
                    {
                        DataList dl = (DataList )c;
                        dl.DataSource = ds;
                        dl.DataBind();
                    }
                    c.Visible = true ;
                }
            }
            else
            {
                c.Visible = false ;
            }
 
        }
private int GetRows( string tablename, string strwhere)
        {
            string strSql = "select count(1) from " + tablename + " with(nolock) where " + strwhere + "" ;
            return int .Parse( DbHelperSQL.GetSingle(strSql).ToString());
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetList( string tablename, string keyname, string sfields, int  PageSize, int PageIndex, stringstrWhere)
        {
            SqlParameter [] parameters = {
                                                                                 new SqlParameter ( "@tblName", SqlDbType .VarChar, 255),
                                                                                 new SqlParameter ( "@fldName", SqlDbType .VarChar, 255),
                    new SqlParameter ( "@sFields", SqlDbType .VarChar,1000),
                                                                                 new SqlParameter ( "@PageSize", SqlDbType .Int),
                                                                                 new SqlParameter ( "@PageIndex", SqlDbType .Int),
                                                                                 new SqlParameter ( "@IsReCount", SqlDbType .Bit),
                                                                                 new SqlParameter ( "@OrderType", SqlDbType .Bit),
                                                                                 new SqlParameter ( "@strWhere", SqlDbType .VarChar,1000),
                                                                                };
            parameters[0].Value = tablename;
            parameters[1].Value = keyname;
            parameters[2].Value = sfields;
            parameters[3].Value = PageSize;
            parameters[4].Value = PageIndex;
            parameters[5].Value = 0;
            parameters[6].Value = 1;
            parameters[7].Value = strWhere;
            return DbHelperSQL .RunProcedure( "Up_GetRecordByPage", parameters, "ds" );
        }
这是在DbHelperSQL 中的方法:
  /// <summary>
    /// 执行存储过程返回dataset
    /// </summary>
    /// <param name="storedProcName"> 存储过程名 </param>
    /// <param name="parameters"> 存储过程参数 </param>
    /// <param name="tableName"> DataSet结果中的表名 </param>
    /// <returns> DataSet</returns>
    public static DataSet RunProcedure( string storedProcName, IDataParameter [] parameters, string tableName)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            DataSet dataSet = new DataSet();
            connection.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();
            sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
            sqlDA.Fill(dataSet, tableName);
            connection.Close();
            return dataSet;
        }
    }
 
    /// <summary>
    /// 执行一条计算查询结果语句,返回查询结果(object)。
    /// </summary>
    /// <param name="SQLString"> 计算查询结果语句 </param>
    /// <returns> 查询结果(object) </returns>
    public static object GetSingle( string SQLString)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand(SQLString, connection))
            {
                try
                {
                    connection.Open();
                    object obj = cmd.ExecuteScalar();
                    if ((Object .Equals(obj, null)) || ( Object .Equals(obj, System.DBNull .Value)))
                    {
                        return null ;
                    }
                    else
                    {
                        return obj;
                    }
                }
                catch (System.Data.SqlClient.SqlException e)
                {
                    connection.Close();
                    connection.Dispose();
 
                    throw new Exception(e.Message);
                }
 
            }
        }
    }
这是Sear()方法的代码:
         /// <summary>
        /// 根据传值进行搜索分页
        /// </summary>
        /// <returns></returns>
        private string Sear()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append( "1=1" );
            if (ParentTypeId != 0)
                sb.AppendFormat( " and ptypeid={0}" ,ParentTypeId);
            if (TypeId != 0)
                sb.AppendFormat( " and typeid={0}" ,TypeId);
            if (GradeId != 0)
                sb.AppendFormat( " and gid={0}" , GradeId);
            return sb.ToString();
        }
这是获取分页中显示的内容代码:
  private string GetFields()
        {
            return "id,title,addtime,Content,typeid,ptypeid,views" ;
        }
 
接着添加分页的事件:
private void ZuowenHotBind()
        {
            DataSet ds = ZuowenDal .GetList(10, "addtime>'" + DateTime .Now.AddMonths(-3).ToString() + "'" , "views desc" );
            if (ds.Tables[0].Rows.Count > 0)
            {
                RepHot.DataSource = ds;
                RepHot.DataBind();
            }
        }
以上操作基本完成了aspnetpager分页是实现,又不懂的问题可以直接评论回复。
(代码用到了封装的DbHelperSQL.cs)

.net中实现aspnetpager分页的更多相关文章

  1. AspNetPager分页控件配置

    AspNetPager是asp.net中常用的分页控件,下载AspNetPager.dll,添加引用,在工具栏就可以看到AspNetPager控件: 拖过来之后,设置如下属性: <webdiye ...

  2. AspNetPager分页控件

    AspNetPager分页控件解决了分页中的很多问题,直接采用该控件进行分页处理,会将繁琐的分页工作变得简单化,下面是我如何使用AspNetPager控件进行分页处理的详细代码:1.首先到www.we ...

  3. 【转】AspNetPager分页控件用法

    AspNetPager分页控件解决了分页中的很多问题,直接采用该控件进行分页处理,会将繁琐的分页工作变得简单化,下面是我如何使用AspNetPager控件进行分页处理的详细代码: 1.首先到www.w ...

  4. 【Bootstrap3.0建站笔记三】AspNetPager分页,每一列都可排序

    1.AspNetPager分页,实现每一列都可排序:           (1).须要将默认排序字段放在HTML页面中.           (2).排序字段放置为td节点的属性. 如图: 实现的效果 ...

  5. NHibernate初学三之条件查询(Criteria Queries)与AspNetPager分页实例

    NHibernate除了SQL与HQL两种查询操作外,还有一种就是条件查询Criteria,本文将从网上整理一些Criteria的理论及小实例,最后通过一个结合AspNetPager分页来加深理解,必 ...

  6. SpringMVC结合easyUI中datagird实现分页

    SpringMVC结合easyUI中datagird实现分页 DataGrid以表格形式展示数据,并提供了丰富的选择.排序.分组和编辑数据的功能支持.轻量级,单元格合并.多列标题.冻结列和页脚只是其中 ...

  7. AspNetPager分页

    1.页面部分 <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefi ...

  8. 给AspNetPager分页控件添加bootstrap样式

    AspNetPager分页控件算是比较好用的一个分页控件了.想要结合bootstrap使用,官方代码入口 .pagination a[disabled]{ color: #777;cursor: no ...

  9. sf中标准的分页功能介绍

    世上本无事,庸人自扰之.我喜欢一个相对比较安静的环境去学习和工作,希望在一个掉一根针的声音都能够听到的环境中,但是有时候往往相反,一片嘈杂,我改变不了周围的环境,只能改变自己,其实这些都没有什么,也许 ...

随机推荐

  1. Self-Attetion

    四.self-attention 1.是什么? attention机制通常用在encode与decode之间,但是self-attention则是输入序列与输出序列相同,寻找序列内部元素的关系即 K= ...

  2. Node.js之错误处理

    Node.js之错误处理 1. 使用 domain 模块处理错误 try..catch 多用于捕捉同步方法中的抛出错误,但不能用try..catch捕捉异步方法中抛出de错误 如: 1 var htt ...

  3. BZOJ 1641 USACO 2007 Nov. Cow Hurdles 奶牛跨栏

    [题解] 弗洛伊德.更新距离的时候把$f[i][j]=min(f[i][j],f[i][k]+f[k][j])$改为$f[i][j]=min(f[i][j],max(f[i][k],f[k][j])) ...

  4. [USACO16OPEN]关闭农场Closing the Farm(洛谷 3144)

    题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...

  5. 网页title上面添加图片

    1.效果:

  6. hdu_1012_u Calculate e_201310121519

    u Calculate eTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. N天学习一个Linux命令之hostnamectl

    前言 安装了CentOS7,发现按照以前修改文件/etc/sysconfig/network HOSTNAME字段主机名的方式不生效了,查资料发现可以使用hostnamectl命令 用途 Contro ...

  8. HDU 4523

    很简单了, 当m>=3&&m<=n+p时是yes import java.math.BigInteger; import java.util.Scanner; public ...

  9. Think in ISP(image sensor pipe )之How to implement an effecitive AE

    How to implement a effecitive AE. AE(自己主动曝光) 1)try Minual exposure //time,line 2)try Max exposure // ...

  10. Java基础之hashCode方法具体解释

    想要明确hashCode的作用,必须要先知道java中的集合.(不明确的请看Java基础之集合框架具体解释(二)List篇和Java基础之集合框架具体解释(三)Set篇) Java中的Collecti ...