ASP.NET的分頁方法(二)

來源:互聯網
上載者:User

標籤:分頁控制項   style   blog   color   io   os   使用   ar   for   

第二講主要使用到了常用的分頁控制項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的分頁方法(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.