欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET存储过程实现分页效果(三层架构)

程序员文章站 2023-12-17 20:22:28
本文实例为大家分享了asp.net存储过程实现分页的具体代码,供大家参考,具体内容如下 实现效果: 文本框内输入跳转的页数,点击go会跳转到该页 首先在项目下加入...

本文实例为大家分享了asp.net存储过程实现分页的具体代码,供大家参考,具体内容如下

实现效果:
文本框内输入跳转的页数,点击go会跳转到该页

ASP.NET存储过程实现分页效果(三层架构)

首先在项目下加入bll,dal,dataaccess,model类库
1、前台界面

<%@ page language="c#" autoeventwireup="true" codebehind="原始刷新分页.aspx.cs" inherits="分页.原始刷新分页" %>
<!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 () {
   $('#txtpageindex').focus(function () {
    $(this).val("");
   })
  })
 </script>
</head>
<body>
 <form id="form1" runat="server">
 <div>
  <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false">
   <columns>
    <asp:boundfield datafield="id" headertext="编号" />
    <asp:boundfield datafield="newstitle" headertext="新闻标题" />
    <asp:boundfield datafield="newscontent" headertext="新闻内容" />
    <asp:boundfield datafield="createtime" 
     dataformatstring="{0:yyyy-mm-dd hh:mm:ss}" headertext="创建时间" />
   </columns>
  </asp:gridview>
 </div>
 <div>
   <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="txtpageindex" runat="server"></asp:textbox>
  <asp:linkbutton id="linkbutton5" runat="server" onclick="linkbutton5_click">go</asp:linkbutton>
 </div>
 </form>
</body>
</html>

2、后台代码

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 分页
{
 public partial class 原始刷新分页 : system.web.ui.page
 {
  int pagesize = 10;
  protected void page_load(object sender, eventargs e)
  {
   
   if (!ispostback)
   {
    viewstate["pageindex"] = 1;
    getlastpageindex();
    loaddata();
   }
  }

  private void getlastpageindex()
  {
   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 loaddata()
  {
   bll.t_news1 bnews = new bll.t_news1();
   datatable dt = bnews.getlistdatatable(pagesize,convert.toint32(viewstate["pageindex"]));
   this.gridview1.datasource = dt;
   this.gridview1.databind();
   
   }
  protected void btnfirst_click(object sender, eventargs e)
  {
   viewstate["pageindex"] = 1;
   loaddata();
  }
  protected void btnpre_click(object sender, eventargs e)
  {
   int pageindex = convert.toint32(viewstate["pageindex"]);
   if (pageindex > 1)
   {
    pageindex--;
    viewstate["pageindex"] = pageindex;
    loaddata();
   }


  }
  protected void btnnext_click(object sender, eventargs e)
  {
   int pageindex = convert.toint32(viewstate["pageindex"]);
   if (pageindex < convert.toint32(viewstate["lastpageindex"]))
   {
    pageindex++;
    viewstate["pageindex"] = pageindex;
    loaddata();
   }
  }
  protected void btnlast_click(object sender, eventargs e)
  {
    viewstate["pageindex"] = viewstate["lastpageindex"];
   loaddata();
  }
  
  protected void linkbutton5_click(object sender, eventargs e)
  {
   int result;
   if (int.tryparse(txtpageindex.text, out result) == true)
   {
    viewstate["pageindex"] = txtpageindex.text.trim();
    loaddata();
   }
   else { txtpageindex.text = "请输入合法的数字"; }
  }
 }
}

3、数据库存储过程

declare @pagesize int;
declare @pageindex int;
select * from (select row_number() over(order by id) as rownumber,* from t_news1)t
where rownumber>(@pageindex-1)*@pagesize and rownumber<=@pagesize*@pageindex

go 
create proc pro_fenye
@pagesize int,
@pageindex int

as
select * from(select row_number() over(order by id) as rownumber,* from t_news1)t 
where rownumber>(@pageindex-1)*@pagesize and rownumber<=@pageindex*@pagesize
go
exec pro_fenye 2,5

以上就是本文的全部内容,希望对大家的学习有所帮助。

上一篇:

下一篇: