Front desk design:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="" Inherits="May 21 Exercise.paging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head runat="server">
<title></title>
<script src="js/Jquery1." type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#txtPagination').focus(function () {
$(this).val("");
})
})
</script>
</head>
<body>
<form 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="Published Time" />
</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 stored procedures 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 between (@pageSize*(@pageIndex-1)+1) and @pageIndex*@pageSize
end
exec usp_role_GetDateByPageIndex 5,3
Add BLL, DAL, DataAccess, MODEL layers to the project
Write a method in DAL:
//The method you write by yourself, get the data list by paging
public DataTable GetListDataTable(int PageSize, int PageIndex)
{
SqlParameter[] parameters = {
new SqlParameter("@PageSize", ),
new SqlParameter("@PageIndex", )
};
parameters[0].Value = PageSize;
parameters[1].Value = PageIndex;
return ("usp_role_GetDateByPageIndex", parameters);
}
Call GetListDataTable in BLL:
public DataTable GetListDataTable(int pagesize, int pageindex)
{
return (pagesize, pageindex);
}
Add RunProcedureDataTable method in DbHelper:
public static DataTable RunProcedureDataTable(string stroreProcName, IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
();
SqlDataAdapter sqlDA = new SqlDataAdapter();
= BuildQueryCommand(connection, stroreProcName, parameters);
(dt);
();
return dt;
}
}
Then call it in the background:
using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace practice
{
public partial class paging :
{
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 = ("");
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 = (pagesize, Convert.ToInt32(ViewState["pageindex"]));
this. = dt;
this.();
}
//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();
}
}
//The last page
protected void btnLast_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = ViewState["lastpageindex"];
LadaData();
}
//Jump page
protected void btnSkip_Click(object sender, EventArgs e)
{
int result;
if ((, out result) == true)
{
ViewState["pageindex"] = ();
LadaData();
}
else
{
= "Please enter a legal number";
}
}
}
}
Copy the codeThe code is as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="" Inherits="May 21 Exercise.paging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head runat="server">
<title></title>
<script src="js/Jquery1." type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#txtPagination').focus(function () {
$(this).val("");
})
})
</script>
</head>
<body>
<form 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="Published Time" />
</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 stored procedures in the database
Copy the codeThe code is as follows:
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 between (@pageSize*(@pageIndex-1)+1) and @pageIndex*@pageSize
end
exec usp_role_GetDateByPageIndex 5,3
Add BLL, DAL, DataAccess, MODEL layers to the project
Write a method in DAL:
Copy the codeThe code is as follows:
//The method you write by yourself, get the data list by paging
public DataTable GetListDataTable(int PageSize, int PageIndex)
{
SqlParameter[] parameters = {
new SqlParameter("@PageSize", ),
new SqlParameter("@PageIndex", )
};
parameters[0].Value = PageSize;
parameters[1].Value = PageIndex;
return ("usp_role_GetDateByPageIndex", parameters);
}
Call GetListDataTable in BLL:
public DataTable GetListDataTable(int pagesize, int pageindex)
{
return (pagesize, pageindex);
}
Add RunProcedureDataTable method in DbHelper:
public static DataTable RunProcedureDataTable(string stroreProcName, IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
();
SqlDataAdapter sqlDA = new SqlDataAdapter();
= BuildQueryCommand(connection, stroreProcName, parameters);
(dt);
();
return dt;
}
}
Then call it in the background:
Copy the codeThe code is as follows:
using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace practice
{
public partial class paging :
{
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 = ("");
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 = (pagesize, Convert.ToInt32(ViewState["pageindex"]));
this. = dt;
this.();
}
//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();
}
}
//The last page
protected void btnLast_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = ViewState["lastpageindex"];
LadaData();
}
//Jump page
protected void btnSkip_Click(object sender, EventArgs e)
{
int result;
if ((, out result) == true)
{
ViewState["pageindex"] = ();
LadaData();
}
else
{
= "Please enter a legal number";
}
}
}
}