简单的aspx页面分页技术

HTML code
  
  
<% @ Page Language = " C# " %> <% @ Import Namespace = " System.Data.SqlClient " %> <% @ Import Namespace = " System.Data " %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < script runat ="server" > private string ConnectionString = @ " server=.;uid=sa;pwd=000000;database=NorthWind " ; private System.Text.StringBuilder output = new StringBuilder(); private DataSet BindPageData( int CurrentPage, int PageSize, string SqlCommandText) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = " SYS_PageDate " ; cmd.Parameters.AddWithValue( " @sqlstr " , SqlCommandText).SqlDbType = SqlDbType.NVarChar; cmd.Parameters.AddWithValue( " @currentpage " , CurrentPage).SqlDbType = SqlDbType.Int; cmd.Parameters.AddWithValue( " @pagesize " , PageSize).SqlDbType = SqlDbType.Int; DataSet ds = new DataSet(); using (SqlDataAdapter dapter = new SqlDataAdapter(cmd)) { dapter.Fill(ds); } return ds; } } void SelectPager( int PageIndex, int PageCount, int TotalRecord) { int previous = 1 ; int next = 0 ; int step = 5 ; int start, end; if (PageIndex < PageCount) { next = PageIndex + 1 ; } else { PageIndex = PageCount; previous = PageCount - 1 ; next = PageCount; } start = 1 ; end = PageIndex + step; if (PageIndex > step) { start = PageIndex - step; } if (end > PageCount) { end = PageCount; } else { end = end - 1 ; } output.Append( " <div class=/"selectpage/"> " ); if (PageIndex > start) // 如果不是首页 { previous = PageIndex - 1 ; output.AppendFormat( " <a href=/"{0}?page=1/">首页</a> " , Request.FilePath); output.AppendFormat( " <a href=/"{0}?page={1}/">上一页</a> " , Request.FilePath, previous); } for ( int i = start; i <= end; i ++ ) { if (PageIndex == i) { output.AppendFormat( " <span class=/"selectpageindex/">&nbsp;{0}&nbsp;</span> " , i); } else { output.AppendFormat( " <a href=/"{0}?page={1}/" class=/"pageindex/">&nbsp;{1}&nbsp;</a> " , Request.FilePath, i); } } if (PageIndex < end) { output.AppendFormat( " <a href=/"{0}?page={1}/">下一页</a> " , Request.FilePath, next); output.AppendFormat( " <a href=/"{0}?page={1}/">尾页</a> " , Request.FilePath, PageCount); } output.AppendFormat( " 当前第<font color=red>{0}</font>页 共<font color=red>{1}</font>页 <font color=red>{2}</font>条数据 " , PageIndex, PageCount, TotalRecord); output.Append( " </div> " ); } string ShowData( int PageIndex) { string Sql = @ " select * from Customers " ; DataSet ds = BindPageData(PageIndex, 15 , Sql); DataTable dt; if (ds.Tables.Count == 0 ) { dt = ds.Tables[ 0 ]; } else { dt = ds.Tables[ 2 ]; if (dt.Rows.Count == 0 && PageIndex > 1 ) // 如果输入的当前页数超出总页数 { ds = BindPageData(Convert.ToInt32(ds.Tables[ 1 ].Rows[ 0 ][ 0 ]), 15 , Sql); dt = ds.Tables[ 2 ]; } } dt.Columns.Remove( " ROWSTAT " ); SelectPager(PageIndex, Convert.ToInt32(ds.Tables[ 1 ].Rows[ 0 ][ 0 ]), Convert.ToInt32(ds.Tables[ 1 ].Rows[ 0 ][ 1 ])); output.Append( " <table class=/"tb/" border=/"1/" cellpadding=/"0/" cellspacing=/"0/" > " ); output.Append( " <tr class=/"tb_Header/"> " ); foreach (DataColumn column in dt.Columns) { output.Append( " <td> " + column.ColumnName + " </td> " ); } output.Append( " </tr> " ); foreach (DataRow row in dt.Rows) { output.Append( " <tr class=/"tb_Content/"> " ); for ( int i = 0 ; i < dt.Columns.Count; i ++ ) { output.Append( " <td> " ); output.Append(row[dt.Columns[i].ColumnName]); output.Append( " </td> " ); } output.Append( " </tr> " ); } output.Append( " </table> " ); return output.ToString(); } protected override void OnUnload(EventArgs e) { this .output = null ; } </ script > < html xmlns ="http://www.w3.org/1999/xhtml" > < head runat ="server" > < title > 未命名頁面 </ title > < style type ="text/css" > a:link { font-size : 9pt ; color : #3E94F1 ; text-decoration : none ; } a:visited { color : #000fff ; text-decoration : none ; } a:hover { color : red ; text-decoration : underline } a:active { text-decoration : none } .selectpage { font-size : 9pt ; color : #3E94F1 ; width : 100% ; text-align : center } .selectpageindex { color : #fff ; background-color : #3E94F1 ; padding : 1px ; width : 20px ; margin-right : 5px } .pageindex { border : solid 1px #ccc ; padding : 1px ; margin-right : 7px } .tb { width : 100% ; border-collapse : collapse } .tb_Header { font-size : 12px ; font-weight : bold ; color : #3E94F1 ; texta-align : center } .tb_Content { font-size : 9pt ; color : #3E9345 } body { margin : 0px } ul { margin : 0px } ul li { margin : 0px ; display : inline ; } </ style > </ head > < body > < form id ="form1" runat ="server" > <% = ShowData(Request.QueryString[ " page " ] == null ? 1 : Convert.ToInt32(Request.QueryString[ " page " ])) %> </ form > </ body > </ html >

 

 

--SQL code

 

CREATE   PROCEDURE   SYS_PageDate 
(
   
@sqlstr   nvarchar(4000),
   
@currentpage   int,   --頁次
    @pagesize   int   --行數
)
AS  
set   nocount   on  
DECLARE
   
@P1        int,   --P1是游標的id  
    @rowcount    int  

SET    @sqlstr=replace(replace(replace(replace(replace(replace(replace(replace(@sqlstr,'exec ',''),'insert ',''),'delete ',''),'update ',''),'drop ',''),'alter ',''),'chr(',''),'mid(','')

exec   sp_cursoropen   @P1  output ,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount   output  

--    頁數 行數
SELECT   ceiling(1.0*@rowcount/@pagesize)   AS   PageCount,@rowcount AS RowCounts  --as   ?行?,@currentpage   as  
set   @currentpage=(@currentpage-1)*@pagesize+1  

exec   sp_cursorfetch   @P1,16,@currentpage,@pagesize  

exec   sp_cursorclose   @P1  

set   nocount   off
GO

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值