当前位置:Gxlcms > asp.net > 三层+存储过程实现分页示例代码

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

时间:2021-07-01 10:21:17 帮助过:8人阅读

前台设计:
代码如下:
  1. <br><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="paging.aspx.cs" Inherits="五二一练习.paging" %> <br><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br><html xmlns="http://www.w3.org/1999/xhtml"> <br><head runat="server"> <br><title></title> <br><script src="js/Jquery1.7.js" type="text/javascript"></script> <br><script type="text/javascript"> <br>$(function () { <br>$('#txtPagination').focus(function () { <br>$(this).val(""); <br>}) <br>}) <br></script> <br></head> <br><body> <br><form id="form1" runat="server"> <br><div> <br><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" <br>Height="336px" Width="685px"> <br><Columns> <br><asp:BoundField DataField="Id" HeaderText="编号" /> <br><asp:BoundField DataField="NewsTitle" HeaderText="标题" /> <br><asp:BoundField DataField="NewsContent" HeaderText="内容" /> <br><asp:BoundField DataField="CreateTime" <br>DataFormatString="{0:yyyy-MM-dd hh:mm:ss}" HeaderText="发布时间" /> <br></Columns> <br></asp:GridView> <br><asp:LinkButton ID="btnFirst" runat="server" onclick="btnFirst_Click">第一页</asp:LinkButton> <br><asp:LinkButton <br>ID="btnPre" runat="server" onclick="btnPre_Click">上一页</asp:LinkButton> <br><asp:LinkButton ID="btnNext" <br>runat="server" onclick="btnNext_Click">下一页</asp:LinkButton> <br><asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最后一页</asp:LinkButton><asp:TextBox <br>ID="txtPagination" runat="server"></asp:TextBox> <br><asp:LinkButton ID="btnSkip" runat="server" onclick="btnSkip_Click">GO</asp:LinkButton> <br></div> <br></form> <br></body> <br></html> <br> <br>首先在数据库创建存储过程 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>create proc usp_role_GetDateByPageIndex <br>@pageSize int, <br>@pageIndex int <br>as <br>begin <br>select * from <br>( <br>select *,ROW_NUMBER() over(order by role_id) as rownumber from role) as tbl <br>where tbl.rownumber between (@pageSize*(@pageIndex-1)+1) and @pageIndex*@pageSize <br>end <br>exec usp_role_GetDateByPageIndex 5,3 <br> <br>在项目中添加BLL,DAL,DataAccess,MODEL层 <br>在DAL中写一个方法: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//自己写的方法,分页获取数据列表 <br>public DataTable GetListDataTable(int PageSize, int PageIndex) <br>{ <br>SqlParameter[] parameters = { <br>new SqlParameter("@PageSize", SqlDbType.Int), <br>new SqlParameter("@PageIndex", SqlDbType.Int) <br>}; <br>parameters[0].Value = PageSize; <br>parameters[1].Value = PageIndex; <br>return DbHelperSQL.RunProcedureDataTable("usp_role_GetDateByPageIndex", parameters); <br>} <br>在BLL中调用GetListDataTable: <br>public DataTable GetListDataTable(int pagesize, int pageindex) <br>{ <br>return dal.GetListDataTable(pagesize, pageindex); <br>} <br>在DbHelper中添加RunProcedureDataTable方法: <br>public static DataTable RunProcedureDataTable(string stroreProcName, IDataParameter[] parameters) <br>{ <br>using (SqlConnection connection = new SqlConnection(connectionString)) <br>{ <br>DataTable dt = new DataTable(); <br>connection.Open(); <br>SqlDataAdapter sqlDA = new SqlDataAdapter(); <br>sqlDA.SelectCommand = BuildQueryCommand(connection, stroreProcName, parameters); <br>sqlDA.Fill(dt); <br>connection.Close(); <br>return dt; <br>} <br>} <br> <br>然后在后台调用即可: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>using System; <br>using System.Collections.Generic; <br>using System.Linq; <br>using System.Web; <br>using System.Web.UI; <br>using System.Web.UI.WebControls; <br>using System.Data; <br>namespace 练习 <br>{ <br>public partial class paging : System.Web.UI.Page <br>{ <br>int pagesize = 10; <br>int pageindex = 1; <br>protected void Page_Load(object sender, EventArgs e) <br>{ <br>if (!IsPostBack) <br>{ <br>ViewState["pageindex"] = 1; <br>LadaData(); <br>GetListPageindex(); <br>} <br>} <br>private void GetListPageindex() <br>{ <br>BLL.T_News1 bnews = new BLL.T_News1(); <br>int totalcount = bnews.GetRecordCount(""); <br>if (totalcount % pagesize == 0) <br>{ <br>ViewState["lastpageindex"] = totalcount / pagesize; <br>} <br>else <br>{ <br>ViewState["lastpageindex"] = totalcount / pagesize + 1; <br>} <br>} <br>private void LadaData() <br>{ <br>BLL.T_News1 bnews = new BLL.T_News1(); <br>DataTable dt = bnews.GetListDataTable(pagesize, Convert.ToInt32(ViewState["pageindex"])); <br>this.GridView1.DataSource = dt; <br>this.GridView1.DataBind(); <br>} <br>//第一页 <br>protected void btnFirst_Click(object sender, EventArgs e) <br>{ <br>ViewState["pageindex"] = 1; <br>LadaData(); <br>} <br>//上一页 <br>protected void btnPre_Click(object sender, EventArgs e) <br>{ <br>int pageindex = Convert.ToInt32(ViewState["pageindex"]); <br>if (pagesize>1) <br>{ <br>pageindex--; <br>ViewState["pageindex"] = pageindex; <br>LadaData(); <br>} <br>} <br>//下一页 <br>protected void btnNext_Click(object sender, EventArgs e) <br>{ <br>int pageindex = Convert.ToInt32(ViewState["pageindex"]); <br>if (pageindex<Convert.ToInt32(ViewState["lastpageindex"])) <br>{ <br>pageindex++; <br>ViewState["pageindex"] = pageindex; <br>LadaData(); <br>} <br>} <br>//最后一页 <br>protected void btnLast_Click(object sender, EventArgs e) <br>{ <br>ViewState["pageindex"] = ViewState["lastpageindex"]; <br>LadaData(); <br>} <br>//跳转页面 <br>protected void btnSkip_Click(object sender, EventArgs e) <br>{ <br>int result; <br>if (int.TryParse(txtPagination.Text, out result) == true) <br>{ <br>ViewState["pageindex"] = txtPagination.Text.Trim(); <br>LadaData(); <br>} <br>else <br>{ <br>txtPagination.Text = "请输入合法的数字"; <br>} <br>} <br>} <br>} <br></li><li> </li><li> </li></ol></pre></li></ol></pre></li></ol></pre>

人气教程排行