AspNetPager+GridView實現分頁的執行個體代碼

來源:互聯網
上載者:User

.架構是.NET Framework 4.0
.一共為三個部分: 前台頁面設計代碼、前台頁面程式碼、css樣式
.其中資料庫連接操作用了DB類(串連語句),SQLHelper(微軟的資料庫操作類)

前台頁面設計代碼

複製代碼 代碼如下:<%@ 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)
{
//調用綁定分頁和GridView
BindGridView();
}
}

////綁定分頁和GridView方法
private void BindGridView()
{
//查詢語句
string sequal = "select StandardName as 標準名稱, MakeUpItem as 補償項目, Unit as 單位,"
+ " cast(UnitPrice as decimal(18,2)) as 單價, cast(StandRate as decimal(18,2)) as "
+ "成新率, Type as 分類 from Standard";
//擷取資料表格
DataTable dt =
SqlHelper.ExecuteDataset(DB.con, CommandType.Text, sequal).Tables[0];
//初始化分頁資料來源執行個體
PagedDataSource pds = new PagedDataSource();
//設定總行數
AspNetPager1.RecordCount = dt.Rows.Count;
//設定分頁的資料來源
pds.DataSource = dt.DefaultView;
//設定當前頁
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
//設定每頁顯示頁數
pds.PageSize = AspNetPager1.PageSize;
//啟用分頁
pds.AllowPaging = true;
//設定GridView的資料來源為分頁資料來源
GridView1.DataSource = pds;
//綁定GridView
GridView1.DataBind();
}

protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
//調用綁定分頁和GridView
BindGridView();
}
}
}

CSS樣式複製代碼 代碼如下:.anpager
{
font: 11px Arial, Helvetica, sans-serif;
padding:10px 20px 10px 0;
margin: 0px;
}
.anpager a
{
padding: 1px 6px;
border: solid 1px #ddd;
background: #fff;
text-decoration: none;
margin-right:2px
}
.anpager a:visited
{
padding: 1px 6px;
border: solid 1px #ddd;
background: #fff;
text-decoration: none;
}
.anpager .cpb
{
padding: 1px 6px;
font-weight: bold;
font-size: 13px;
border:none
}
.anpager a:hover
{
color: #fff;
background: #ffa501;
border-color:#ffa501;
text-decoration: none;
}

/* AspNetPager1屬性設定: CssClass="anpager" CurrentPageButtonClass="cpb"*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.