注意:
1.首先到www.webdiyer.com下載最新的AspNetPager.dll,直接在vs2005中添加引用即可。
2.添加工具箱
3.註冊(<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>)
使用AspNetPager和GridView實現分頁的簡單例子
1.首先 簡單介紹一下AspNetPager這個分頁組件AspNetPager控件和數據是獨立的,將分頁導航功能與數據顯示功能完全獨立開來,由用戶自己控制數據的獲取及顯示方式,因此可以被靈活地使用,以及和GridView、DataList、Repeater等數據綁定控件幾何起來實現分頁、呈現自定義的分頁數據。 2. AspNetPager的基本屬性 AlwaysShow:
默認設置爲false,將此屬性值設爲true時,即使總頁數只有一頁,AspNetPager也將顯示分頁導航元素。 FirstPageText:
獲取或設置爲第一頁按鈕顯示的文本。 PrevPageText:
獲取或設置爲上一頁按鈕顯示的文本。 NextPageText:
獲取或設置爲下一頁按鈕顯示的文本。 LastPageText:
獲取或設置爲最後一頁按鈕顯示的文本。 PageSize:
獲取或設置每頁顯示的項數。(該值獲取或設置數據呈現控件每次要顯示數據表中的的數據的項數,AspNetPager根據該值和 RecordCount 來計算顯示所有數據需要的總頁數,即 PageCount的值。 )
CssClass:
應用於控件的css類名,這個要自己事先設定好CSS CurrentPageButtonClass:
獲取或設置AspNetPager分頁控件當前頁導航按鈕的級聯樣式表 (CSS) 類。
PageIndexBoxType:
或者或設置頁索引框的顯示類型,可以是允許用戶手工輸入的文本框和只能選擇的下拉框。(這個可以有textbox 或者dropdownlist兩種方式,需要在後臺代碼中觸發事件的) ShowBoxThreshold:
ShowPageIndexBox默認設置爲auto,其默認值是30,,只有要分頁的數據的總頁數達到該值時會自動顯示頁索引輸入文本框。該選項當 ShowPageIndexBox 設爲Never或Always時沒有任何作用。
3.與gridview結合實現的小例子,已經調試過了
前臺代碼如下:
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeBehind="fenye21.aspx.cs"
Inherits="fenye2.fenye21" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> <!DOCTYPE
html
PUBLIC
"-//W3C//DTD
XHTML
1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
<title>使用分頁組件AspNetPager和GridView實現分頁實例</title> </head>
<body>
<p>工作室小組成員信息表</p> <form id="form1" runat="server"> <div><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="4" GridLines="None" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" /> <Columns>
<asp:BoundField DataField="Id" HeaderText="學生ID" /> <asp:BoundField DataField="Name" HeaderText="姓名" /> <asp:BoundField DataField="Sex" HeaderText="性別" /> <asp:BoundField DataField="Age" HeaderText="年齡" /> <asp:BoundField DataField="Address" HeaderText="地址" /> <asp:BoundField DataField="Sdept" HeaderText="系別" /> </Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <PagerSettings Visible="False" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <SortedAscendingCellStyle BackColor="#FDF5AC" /> <SortedAscendingHeaderStyle BackColor="#4D0000" /> <SortedDescendingCellStyle BackColor="#FCF6C0" /> <SortedDescendingHeaderStyle BackColor="#820000" /> </asp:GridView>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" CustomInfoTextAlign="left"
HorizontalAlign="Left" SubmitButtonClass="button" SubmitButtonText="跳轉"
FirstPageText="首頁" LastPageText="尾頁" LayoutType="Div" ShowFirstLast="true"
NextPageText="下一頁" PrevPageText="上一頁"
PageSize="4"
PagingButtonSpacing="2px" AlwaysShow="True" ShowBoxThreshold="1"
CurrentPageButtonPosition="Beginning" ShowMoreButtons="False"
onpagechanged="AspNetPager1_PageChanged">>
</webdiyer:AspNetPager> </div> </form> </body> </html>
後臺代碼如下:
using System;using System.Configuration; using System.Collections;
using System.Web; using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data; using System.Data.SqlClient;
namespace fenye2 {
public partial class fenye21 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
dobind(); } }
public void dobind()
{
SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=Stu;Integrated Security=True");string sql = "select * from Info ";
SqlDataAdapter sda = new SqlDataAdapter(sql,cnn);
DataSet ds = new DataSet();
sda.Fill(ds,"Info");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["Info"].DefaultView;
AspNetPager1.RecordCount = pds.Count;
// AspNetPager1.RecordCount = ds.Tables["Info"].DefaultView.Count;
pds.PageSize = AspNetPager1.PageSize;
GridView1.PageIndex = AspNetPager1.CurrentPageIndex - 1;
this.GridView1.DataSource = pds;
this.GridView1.DataBind(); }
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
dobind(); }
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
Bind();
}
protected string FormatString_Size_13(string str)
{
if (str.Length > 33)
{
str = str.Substring(0, 32) + "...";
}
return str;
}