电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

三层+存储过程实现分页示例代码


发布日期:2022/8/20
 

前台设计

复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pagingaspxcs" Inherits="五二一练习paging" %>
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head runat="server">
<title></title>
<script src="js/Jqueryjs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$(#txtPagination)focus(function () {
$(this)val("");
})
})
</script>
</head>
<body>
<form id="form" runat="server">
<div>
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False"
Height="px" Width="px">
<Columns>
<asp:BoundField DataField="Id" HeaderText="编号" />
<asp:BoundField DataField="NewsTitle" HeaderText="标题" />
<asp:BoundField DataField="NewsContent" HeaderText="内容" />
<asp:BoundField DataField="CreateTime"
DataFormatString="{:yyyyMMdd hh:mm:ss}" HeaderText="发布时间" />
</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 *ROW_NUMBER() over(order by role_id) as rownumber from role) as tbl
where tblrownumber between (@pageSize*(@pageIndex)+) and @pageIndex*@pageSize
end
exec usp_role_GetDateByPageIndex


在项目中添加BLLDALDataAccessMODEL层
在DAL中写一个方法

复制代码 代码如下:
//自己写的方法分页获取数据列表
public DataTable GetListDataTable(int PageSize int PageIndex)
{
SqlParameter[] parameters = {
new SqlParameter("@PageSize" SqlDbTypeInt)
new SqlParameter("@PageIndex" SqlDbTypeInt)
};
parameters[]Value = PageSize;
parameters[]Value = PageIndex;
return DbHelperSQLRunProcedureDataTable("usp_role_GetDateByPageIndex" parameters);
}
在BLL中调用GetListDataTable
public DataTable GetListDataTable(int pagesize int pageindex)
{
return dalGetListDataTable(pagesize pageindex);
}
在DbHelper中添加RunProcedureDataTable方法
public static DataTable RunProcedureDataTable(string stroreProcName IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
connectionOpen();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDASelectCommand = BuildQueryCommand(connection stroreProcName parameters);
sqlDAFill(dt);
connectionClose();
return dt;
}
}


然后在后台调用即可

复制代码 代码如下:


using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemData;
namespace 练习
{
public partial class paging : SystemWebUIPage
{
int pagesize = ;
int pageindex = ;
protected void Page_Load(object sender EventArgs e)
{
if (!IsPostBack)
{
ViewState["pageindex"] = ;
LadaData();
GetListPageindex();
}
}
private void GetListPageindex()
{
BLLT_News bnews = new BLLT_News();
int totalcount = bnewsGetRecordCount("");
if (totalcount % pagesize == )
{
ViewState["lastpageindex"] = totalcount / pagesize;
}
else
{
ViewState["lastpageindex"] = totalcount / pagesize + ;
}
}
private void LadaData()
{
BLLT_News bnews = new BLLT_News();
DataTable dt = bnewsGetListDataTable(pagesize ConvertToInt(ViewState["pageindex"]));
thisGridViewDataSource = dt;
thisGridViewDataBind();
}
//第一页
protected void btnFirst_Click(object sender EventArgs e)
{
ViewState["pageindex"] = ;
LadaData();
}
//上一页
protected void btnPre_Click(object sender EventArgs e)
{
int pageindex = ConvertToInt(ViewState["pageindex"]);
if (pagesize>)
{
pageindex;
ViewState["pageindex"] = pageindex;
LadaData();
}
}
//下一页
protected void btnNext_Click(object sender EventArgs e)
{
int pageindex = ConvertToInt(ViewState["pageindex"]);
if (pageindex<ConvertToInt(ViewState["lastpageindex"]))
{
pageindex++;
ViewState["pageindex"] = pageindex;
LadaData();
}
}
//最后一页
protected void btnLast_Click(object sender EventArgs e)
{
ViewState["pageindex"] = ViewState["lastpageindex"];
LadaData();
}
//跳转页面
protected void btnSkip_Click(object sender EventArgs e)
{
int result;
if (intTryParse(txtPaginationText out result) == true)
{
ViewState["pageindex"] = txtPaginationTextTrim();
LadaData();
}
else
{
txtPaginationText = "请输入合法的数字";
}
}
}
}

上一篇:解决ScrollViewer滚动问题的办法

下一篇:检测用户名是否存在