前台設計
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="paging
<!DOCTYPE html PUBLIC "
<html xmlns="
<head runat="server">
<title></title>
<script src="js/Jquery
<script type="text/javascript">
$(function () {
$(
$(this)
})
})
</script>
</head>
<body>
<form id="form
<div>
<asp:GridView ID="GridView
Height="
<Columns>
<asp:BoundField DataField="Id" HeaderText="編號" />
<asp:BoundField DataField="NewsTitle" HeaderText="標題" />
<asp:BoundField DataField="NewsContent" HeaderText="內容" />
<asp:BoundField DataField="CreateTime"
DataFormatString="{
</Columns>
</asp:GridView>
<asp:LinkButton ID="btnFirst" runat="server" onclick="btnFirst_Click">第一頁</asp:LinkButton>
<asp:LinkButton
ID="btnPre" runat="server" onclick="btnPre_Click">上一頁</asp:LinkButton>
<asp:LinkButton ID="btnNext"
runat="server" onclick="btnNext_Click">下一頁</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最後一頁</asp:LinkButton><asp:TextBox
ID="txtPagination" runat="server"></asp:TextBox>
<asp:LinkButton ID="btnSkip" runat="server" onclick="btnSkip_Click">GO</asp:LinkButton>
</div>
</form>
</body>
</html>
首先在數據庫創建存儲過程
create proc usp_role_GetDateByPageIndex
@pageSize int
@pageIndex int
as
begin
select * from
(
select *
where tbl
end
exec usp_role_GetDateByPageIndex
在項目中添加BLL
在DAL中寫一個方法
//自己寫的方法
public DataTable GetListDataTable(int PageSize
{
SqlParameter[] parameters = {
new SqlParameter("@PageSize"
new SqlParameter("@PageIndex"
};
parameters[
parameters[
return DbHelperSQL
}
在BLL中調用GetListDataTable
public DataTable GetListDataTable(int pagesize
{
return dal
}
在DbHelper中添加RunProcedureDataTable方法
public static DataTable RunProcedureDataTable(string stroreProcName
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
connection
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA
sqlDA
connection
return dt;
}
}
然後在後台調用即可
using System;
using System
using System
using System
using System
using System
using System
namespace 練習
{
public partial class paging : System
{
int pagesize =
int pageindex =
protected void Page_Load(object sender
{
if (!IsPostBack)
{
ViewState["pageindex"] =
LadaData();
GetListPageindex();
}
}
private void GetListPageindex()
{
BLL
int totalcount = bnews
if (totalcount % pagesize ==
{
ViewState["lastpageindex"] = totalcount / pagesize;
}
else
{
ViewState["lastpageindex"] = totalcount / pagesize +
}
}
private void LadaData()
{
BLL
DataTable dt = bnews
this
this
}
//第一頁
protected void btnFirst_Click(object sender
{
ViewState["pageindex"] =
LadaData();
}
//上一頁
protected void btnPre_Click(object sender
{
int pageindex = Convert
if (pagesize>
{
pageindex
ViewState["pageindex"] = pageindex;
LadaData();
}
}
//下一頁
protected void btnNext_Click(object sender
{
int pageindex = Convert
if (pageindex<Convert
{
pageindex++;
ViewState["pageindex"] = pageindex;
LadaData();
}
}
//最後一頁
protected void btnLast_Click(object sender
{
ViewState["pageindex"] = ViewState["lastpageindex"];
LadaData();
}
//跳轉頁面
protected void btnSkip_Click(object sender
{
int result;
if (int
{
ViewState["pageindex"] = txtPagination
LadaData();
}
else
{
txtPagination
}
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/14272.html