ASP.NET的分页方法(二)

第二讲主要使用到了常用的分页控件aspnetpager,这里对他就行一个简单的应用,具体大家可以到杨涛的博客上去寻找相关的DLL,

首先要先引用AspNetPager.dll,然后把这个DLL同时添加入工具箱

接下来前台依然使用repeater控件进行绑定,写法如下:

<form id="form1" runat="server">

        <div>

            <ul style="list-style: none">

                <asp:Repeater ID="Repeater1" runat="server">

                    <ItemTemplate>

                        <li><%#Eval("Title") %></li>

                    </ItemTemplate>

                </asp:Repeater>

            </ul>

        </div>

        <webdiyer:AspNetPager ID="AspNetPager1"  CssClass="paginator" runat="server" OnPageChanged="AspNetPager1_PageChanged">

        </webdiyer:AspNetPager>

    </form>

好看的样式网上也有三种左右(我目前所找到的),其中一种如下:

<style type="text/css">

        /*拍拍网风格*/

        .paginator {

            font: 11px Arial, Helvetica, sans-serif;

            padding: 10px 20px 10px 0;

            margin: 0px;

        }



            .paginator a {

                padding: 1px 6px;

                border: solid 1px #ddd;

                background: #fff;

                text-decoration: none;

                margin-right: 2px;

            }



                .paginator a:visited {

                    padding: 1px 6px;

                    border: solid 1px #ddd;

                    background: #fff;

                    text-decoration: none;

                }



            .paginator .cpb {

                padding: 1px 6px;

                font-weight: bold;

                font-size: 13px;

                border: none;

            }



            .paginator a:hover {

                color: #fff;

                background: #ffa501;

                border-color: #ffa501;

                text-decoration: none;

            }

    </style>

后台的写法 如下,使用的是dataset绑定的数据源,但是似乎没有让引用AspNetPager空间,也没法直接Using AspNetPager这样引用,不过也是画蛇添足啦,没什么大影响。

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



namespace WebApplication1

{

    public partial class WebForm3 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if(!IsPostBack)

            {

                GetDate();

            }

        }



        string ConStr = ConfigurationManager.ConnectionStrings["Connection"].ToString();



        public void GetDate()

        {

            //查出所有数据

            string GetDateSql = "SELECT * FROM [Info]";

            //计算一共有多少条数据,这个有待于接下来计算总页数

            string GetCountDataSql = "SELECT COUNT(*) FROM [Info]";



            using (SqlConnection conn = new SqlConnection(ConStr))

            {

                conn.Open();

                DataSet dt = new DataSet();

                SqlDataAdapter sdt = new SqlDataAdapter(GetDateSql, conn);

                //AspNetPager1.PageSize,一页显示多少数据,AspNetPager1.CurrentPageIndex:获取或设置当前显示页的索引。

                //这一条语句对于dataset是一定要按照这个格式写的,具体的原理我也有点模糊,希望明白的朋友给出一些指点。

                sdt.Fill(dt, (AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1)), AspNetPager1.PageSize,"MYTABLE");

                this.Repeater1.DataSource = dt;

                this.Repeater1.DataBind();



                SqlCommand comm = new SqlCommand(GetCountDataSql, conn);

                //AspNetPager1.RecordCount:获取或设置需要分页的所有记录的总数。

                string GetCountData = comm.ExecuteScalar().ToString();

                this.AspNetPager1.RecordCount =Convert.ToInt32(GetCountData);

            }

        }



        protected void AspNetPager1_PageChanged(object sender, EventArgs e)

        {

            GetDate();

        }

    }

}

 

你可能感兴趣的:(asp.net)