Front-end design:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "paging. aspx. cs" Inherits = "Five, two, one exercises. paging" %>
<! 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>
<Script src = "js/Jquery1.7.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
$ ('# TxtPagination'). focus (function (){
$ (This). val ("");
})
})
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False"
Height = "336px" Width = "685px">
<Columns>
<Asp: BoundField DataField = "Id" HeaderText = "no."/>
<Asp: BoundField DataField = "NewsTitle" HeaderText = "title"/>
<Asp: BoundField DataField = "NewsContent" HeaderText = "content"/>
<Asp: BoundField DataField = "CreateTime"
DataFormatString = "{0: yyyy-MM-dd hh: mm: ss}" HeaderText = "Release Date"/>
</Columns>
</Asp: GridView>
<Asp: LinkButton ID = "btnFirst" runat = "server" onclick = "btnFirst_Click"> page 1 </asp: LinkButton>
<Asp: LinkButton
ID = "btnPre" runat = "server" onclick = "btnPre_Click"> previous page </asp: LinkButton>
<Asp: LinkButton ID = "btnNext"
Runat = "server" onclick = "btnNext_Click"> next page </asp: LinkButton>
<Asp: LinkButton ID = "btnLast" runat = "server" onclick = "btnLast_Click"> last page </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>
First, create a stored procedure in the database
Create proc usp_role_GetDateByPageIndex
@ PageSize int,
@ PageIndex int
As
Begin
Select * from
(
Select *, ROW_NUMBER () over (order by role_id) as rownumber from role) as tbl
Where tbl. rownumber between (@ pageSize * (@ pageIndex-1) + 1) and @ pageIndex * @ pageSize
End
Exec usp_role_GetDateByPageIndex 5, 3
Add the BLL, DAL, DataAccess, and MODEL layers to the project.
Write a method in DAL:
// Self-written method to retrieve the data list by PAGE
Public DataTable GetListDataTable (int PageSize, int PageIndex)
{
SqlParameter [] parameters = {
New SqlParameter ("@ PageSize", SqlDbType. Int ),
New SqlParameter ("@ PageIndex", SqlDbType. Int)
};
Parameters [0]. Value = PageSize;
Parameters [1]. Value = PageIndex;
Return DbHelperSQL. RunProcedureDataTable ("usp_role_GetDateByPageIndex", parameters );
}
Call GetListDataTable in BLL:
Public DataTable GetListDataTable (int pagesize, int pageindex)
{
Return dal. GetListDataTable (pagesize, pageindex );
}
Add the RunProcedureDataTable method to DbHelper:
Public static DataTable RunProcedureDataTable (string stroreProcName, IDataParameter [] parameters)
{
Using (SqlConnection connection = new SqlConnection (connectionString ))
{
DataTable dt = new DataTable ();
Connection. Open ();
SqlDataAdapter sqlDA = new SqlDataAdapter ();
SqlDA. SelectCommand = BuildQueryCommand (connection, stroreProcName, parameters );
SqlDA. Fill (dt );
Connection. Close ();
Return dt;
}
}
Then you can call it in the background:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data;
Namespace exercise
{
Public partial class paging: System. Web. UI. Page
{
Int pagesize = 10;
Int pageindex = 1;
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
ViewState ["pageindex"] = 1;
LadaData ();
GetListPageindex ();
}
}
Private void GetListPageindex ()
{
BLL. T_News1 bnews = new BLL. T_News1 ();
Int totalcount = bnews. GetRecordCount ("");
If (totalcount % pagesize = 0)
{
ViewState ["lastpageindex"] = totalcount/pagesize;
}
Else
{
ViewState ["lastpageindex"] = totalcount/pagesize + 1;
}
}
Private void LadaData ()
{
BLL. T_News1 bnews = new BLL. T_News1 ();
DataTable dt = bnews. GetListDataTable (pagesize, Convert. ToInt32 (ViewState ["pageindex"]);
This. GridView1.DataSource = dt;
This. GridView1.DataBind ();
}
// Page 1
Protected void btnFirst_Click (object sender, EventArgs e)
{
ViewState ["pageindex"] = 1;
LadaData ();
}
// Previous Page
Protected void btnPre_Click (object sender, EventArgs e)
{
Int pageindex = Convert. ToInt32 (ViewState ["pageindex"]);
If (pagesize> 1)
{
Pageindex --;
ViewState ["pageindex"] = pageindex;
LadaData ();
}
}
// Next page
Protected void btnNext_Click (object sender, EventArgs e)
{
Int pageindex = Convert. ToInt32 (ViewState ["pageindex"]);
If (pageindex <Convert. ToInt32 (ViewState ["lastpageindex"])
{
Pageindex ++;
ViewState ["pageindex"] = pageindex;
LadaData ();
}
}
// Last page
Protected void btnLast_Click (object sender, EventArgs e)
{
ViewState ["pageindex"] = ViewState ["lastpageindex"];
LadaData ();
}
// Jump to the page
Protected void btnSkip_Click (object sender, EventArgs e)
{
Int result;
If (int. TryParse (txtPagination. Text, out result) = true)
{
ViewState ["pageindex"] = txtPagination. Text. Trim ();
LadaData ();
}
Else
{
TxtPagination. Text = "enter a valid number ";
}
}
}
}