AspNetPager 的使用

下面选用的是新闻发布系统里用的代码。

SQL 存储过程:

CREATE PROCEDURE procNewsSelectByPager
    @startRecordIndex int,
    @endRecordIndex int
AS
BEGIN
    select * from
    (select row_number() over (order by id) as row, * from news) temp
    where temp.row between @startRecordIndex and @endRecordIndex
END
GO

数据访问层:

//SQLHelper
public DataTable ExecuteQuery(string cmdText,SqlParameter[] paras,CommandType ct)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdText, Getconn());
            cmd.CommandType = ct;
            cmd.Parameters.AddRange(paras);
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }


//NewsDAO
public DataTable SelectByPager(int startRecordIndex, int endRecordIndex)
        {
            DataTable dt = new DataTable();
            SqlParameter[] para = new SqlParameter[] { 
                new SqlParameter("@startRecordIndex",startRecordIndex),
                new SqlParameter("@endRecordIndex",endRecordIndex)
            };
            dt = sqlhelper.ExecuteQuery("procNewsSelectByPager",para, CommandType.StoredProcedure);
            return dt;
        }

UI层:

<!-- 前台代码 -->
<div>
<table>
                <tr><th>ID</th>
                    <th>新闻标题</th>
                </tr>
                <asp:Repeater ID="repTest" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td><%# Eval("id") %></td>
                            <td><a href='../Newscontent.aspx?newsId=<%# Eval("id") %>' target="_blank" title='<%# Eval("title") %>' ><%# StringTruncat(Eval("title").ToString(),21,"...") %></a></td>
                        </tr>                        
                    </ItemTemplate>
                </asp:Repeater>
            </table>
<webdiyer:AspNetPager ID="anp" FirstPageText="首页" LastPageText="尾页"
                PrevPageText="上一页" NextPageText="下一页" SubmitButtonText="Go" 
        TextBeforePageIndexBox="转到" TextAfterPageIndexBox="页" PageIndexBoxType="TextBox" ShowPageIndexBox="Always"
        CustomInfoHTML="共&nbsp;<font color='#FF0000'>%PageCount%</font>&nbsp;页,第&nbsp;<font color='#FF0000'>%CurrentPageIndex%</font>&nbsp;页"
        Font-Size="14px" ShowCustomInfoSection="Left" CustomInfoSectionWidth="25%" PagingButtonSpacing="8px"
         runat="server" onpagechanged="anp_PageChanged">
        </webdiyer:AspNetPager>
</div>
//后台代码
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                Bind();
            }
        }

        protected void anp_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        private void Bind()
        {
            NewsManager nm = new NewsManager();    
            // 设置需要分页的所有数据的总数
            anp.RecordCount = nm.SelectCount();
            // 设置 AspNetPager 每页显示的数
            anp.PageSize = 6;
            // 取出当前页的新闻
            DataTable dt = nm.SelectByPager(anp.StartRecordIndex, anp.EndRecordIndex);
            PagedDataSource pds = new PagedDataSource();
            // 设置 页面最多能显示的数量
            pds.PageSize = anp.PageSize;
            // 是否启用分页
            pds.AllowPaging = true;
            // 绑定数据源
            pds.DataSource = dt.DefaultView;
            repTest.DataSource = pds;
            repTest.DataBind();
        }

关于 AspNetPager 控件的使用:    

anp.RecordCount 的设置应在 anp.EndRecordIndex 的使用之前,例:    

anp.RecordCount = 100;
anp.PageSize = 10;
// 自定义的 取出当前页面要显示的数据 的方法
nm.SelectByPager(anp.StartRecordIndex,anp.EndRecordIndex);

    AspNetPager 和 PagedDataSource 的 PageSize 属性(每页显示的项数)    

当 anp.PageSize < pds.PageSize 时,页面显示的是 anp.PageSize 设置的数;

当 anp.PageSize > pds.PageSize 时,页面显示的是 pds.PageSize 设置的数;    

(个人理解)anp.PageSize 是控件每页显示的数,pds.PageSize 是页面最多能显示的数。

原文地址:https://www.cnblogs.com/error503/p/3525633.html