標籤:分頁控制項 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的分頁方法(二)