%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWebSite.Default" %>
%@ 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>/title>
link href="Styles/Paging.css" rel="stylesheet" type="text/css" />
/head>
body>
form id="form1" runat="server">
div>
asp:GridView ID="GridView1" runat="server" Height="261px" Width="737px"
CellPadding="4" ForeColor="#333333" GridLines="None">
AlternatingRowStyle BackColor="White" />
EditRowStyle BackColor="#2461BF" />
FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
HeaderStyle HorizontalAlign="Left" BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
RowStyle BackColor="#EFF3FB" />
SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
SortedAscendingCellStyle BackColor="#F5F7FB" />
SortedAscendingHeaderStyle BackColor="#6D95E1" />
SortedDescendingCellStyle BackColor="#E9EBEF" />
SortedDescendingHeaderStyle BackColor="#4870BE" />
/asp:GridView>
/div>
webdiyer:AspNetPager ID="AspNetPager1" runat="server"
onpagechanged="AspNetPager1_PageChanged" CssClass="anpager"
CurrentPageButtonClass="cpb" FirstPageText="首頁" LastPageText="尾頁"
NextPageText="后頁" PrevPageText="前頁">
/webdiyer:AspNetPager>
/form>
/body>
/html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestWebSite.Utilities;
using System.Data;
using System.Data.SqlClient;
using Wuqi.Webdiyer;
namespace TestWebSite
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//調(diào)用綁定分頁和GridView
BindGridView();
}
}
////綁定分頁和GridView方法
private void BindGridView()
{
//查詢語句
string sequal = "select StandardName as 標(biāo)準(zhǔn)名稱, MakeUpItem as 補(bǔ)償項(xiàng)目, Unit as 單位,"
+ " cast(UnitPrice as decimal(18,2)) as 單價(jià), cast(StandRate as decimal(18,2)) as "
+ "成新率, Type as 分類 from Standard";
//獲取數(shù)據(jù)表格
DataTable dt =
SqlHelper.ExecuteDataset(DB.con, CommandType.Text, sequal).Tables[0];
//初始化分頁數(shù)據(jù)源實(shí)例
PagedDataSource pds = new PagedDataSource();
//設(shè)置總行數(shù)
AspNetPager1.RecordCount = dt.Rows.Count;
//設(shè)置分頁的數(shù)據(jù)源
pds.DataSource = dt.DefaultView;
//設(shè)置當(dāng)前頁
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
//設(shè)置每頁顯示頁數(shù)
pds.PageSize = AspNetPager1.PageSize;
//啟用分頁
pds.AllowPaging = true;
//設(shè)置GridView的數(shù)據(jù)源為分頁數(shù)據(jù)源
GridView1.DataSource = pds;
//綁定GridView
GridView1.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
//調(diào)用綁定分頁和GridView
BindGridView();
}
}
}