国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

用AspNetPager控件最詳細的分頁實現(xiàn)方法(親自實踐)

2019-11-17 03:48:44
字體:
供稿:網(wǎng)友

祝賀一下~好不容易把分頁做出來了。之前都是用GridView自帶的分頁,版式難看不說,還極不優(yōu)化,一次取出所有記錄,然后進行假分頁。
現(xiàn)在用aspNetPager控件做出的真分頁,就好多了,不過還有改進的地方,SQL語句如果換成存儲過程效率會更高。

首先在SqlHelper.cs(DAL層中的數(shù)據(jù)庫助手類,用于寫可以復用的基本增刪查改方法)中加上以下代碼:

Code
/**//// <summary>
        /// 獲取分頁數(shù)據(jù)
        /// </summary>
        /// <param name="sql">sql語句</param>
        /// <param name="currentPage">當前頁</param>
        /// <param name="pagesize">每頁顯示數(shù)</param>
        /// <param name="recordcount"></param>
        /// <returns></returns>
        public static DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
        {
            openCon();
            sqlDs.Clear();

            sqlDa = new SqlDataAdapter(sql, sqlConn);
            int startRow = (currentPage - 1) * pagesize;
            sqlDa.Fill(sqlDs, startRow, pagesize, "table");
            recordcount = GetPageRecord(sql);
            closeCon();

            return sqlDs;
        }

        //返回總的記錄數(shù)
        public static int GetPageRecord(string sql)
        {
            openCon();
            sql = Regex.Replace(sql, "order by.*", "");
            sql = "select count(*) from (" + sql + ") as temp";
            sqlCmd = new SqlCommand(sql, sqlConn);
            int recordcount = int.Parse(sqlCmd.ExecuteScalar().ToString());
            closeCon();

            return recordcount;
        }

然后在BLL層新建一個PageManager.cs的分頁操作類,封裝一下DAL層方法:

Code
/**//// <summary>
        /// 獲取分頁數(shù)據(jù)
        /// </summary>
        /// <param name="sql">sql語句</param>
        /// <param name="currentPage">當前頁</param>
        /// <param name="pagesize">每頁顯示數(shù)</param>
        /// <param name="recordcount"></param>
        /// <returns></returns>
        public static DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
        {
            return SQLHelper.GetPage(sql, currentPage, pagesize, out recordcount);
        }

asp.net需分頁的數(shù)據(jù)綁定處是這樣的:

Code
<asp:Repeater ID="repNewsList" runat="server">
    <ItemTemplate>
        <tr>
            <td align="center"><a href="list.aspx?caid=<%# Eval("caId") %>"><%# Eval("name") %></a></td>
            <td align="center"><%# Eval("createTime") %></td>
        </tr>
    </ItemTemplate>
</asp:Repeater> 

然后在綁定代碼下方加上分頁控件(當然這個可以隨便放,怎么好看怎么放):

Code
<!--分頁控件-->
        <div style="text-align:center; height:50px; line-height:50px;">
       
            <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" UrlPaging="true"
                FirstPageText="首頁" LastPageText="末頁" NextPageText="下一頁" NumericButtonCount="5"
                onpagechanged="AspNetPager1_PageChanged" PagingButtonSpacing="10px" NumericButtonTextFormatString="[{0}]"
                                TextBeforePageIndexBox="轉(zhuǎn)到" ShowCustomInfoSection="Left"
                CustomInfoHTML="目前是第%CurrentPageIndex%頁 / 總共%PageCount%頁">
            </webdiyer:AspNetPager>
           
        </div>

最后在aspx.cs中加上數(shù)據(jù)的分頁綁定方法(這里的SQL語句要根據(jù)列表顯示的需要進行調(diào)整):

Code
/**//// <summary>
    /// 綁定帶有分頁的新聞列表
    /// </summary>
    public void BindRepeater()
    {
        int caid = int.Parse(Request.QueryString["caid"]);

        string Sql = "select * from news where caId=" + caid + " order by createTime desc";
        int CurrentPage = AspNetPager1.CurrentPageIndex;
        int PageSize = AspNetPager1.PageSize;
        int RecordCount;

        DataSet ds = PageManager.GetPage(Sql, CurrentPage, PageSize, out RecordCount);
        AspNetPager1.RecordCount = RecordCount;

        AspNetPager1.CustomInfoHTML += " 共" + RecordCount + "條新聞</b>";

        repNewsList.DataSource = ds;
        repNewsList.DataBind();
    }

別忘了,在page_load調(diào)用一下:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindRepeater();
        }
    }

還有分頁控件的PageChanged事件里也調(diào)用一下:

protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindRepeater();
    }

最后再補充一個非常漂亮的翻頁樣式,清爽超酷型~:

Code
<style>
   
    .anpager
    {}{
        font-size:12px;
    }
    .anpager .cpb
    {}{
        background:#1F3A87 none repeat scroll 0 0;
        border:1px solid #CCCCCC;
        color:#FFFFFF;
        font-weight:bold;
        margin:5px 4px 0 0;
        padding:4px 5px 0;
    }
    .anpager a
    {}{
        background:#FFFFFF none repeat scroll 0 0;
        border:1px solid #CCCCCC;
        color:#1F3A87;
        margin:5px 4px 0 0;
        padding:4px 5px 0;
        text-decoration:none
    }
    .anpager a:hover
    {}{
        background:#1F3A87 none repeat scroll 0 0;
        border:1px solid #1F3A87;
        color:#FFFFFF;
    }

</style>

然后在AspNetPager中加上以下四個屬性,搞定!
CSSClass="anpager"
CurrentPageButtonClass="cpb"
CustomInfoClass=""
CustomInfoTextAlign="Left"

OK,分頁大功告成。 本人QQ:3053166 希望和.net愛好者交流~


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天峨县| 晋城| 岚皋县| 泰州市| 五常市| 化隆| 来宾市| 彭山县| 霍州市| 民权县| 常熟市| 利川市| 临邑县| 渑池县| 潢川县| 塔河县| 丰台区| 普洱| 绥德县| 青神县| 彰化县| 扶风县| 黎川县| 边坝县| 嵊州市| 张掖市| 随州市| 宾川县| 长子县| 汝阳县| 龙南县| 察雅县| 改则县| 祥云县| 漳州市| 崇仁县| 中方县| 中方县| 泰顺县| 新密市| 星子县|