asp.net(C#)海量数据表高效率分页算法(易懂,不…

2008-02-22 09:31:22来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

首先创建一张表(要求ID自动编号):
create table redheadedfile(
id int identity(1,1),
filenames nvarchar(20),
senduser nvarchar(20),
primary key(id)
)
然后我们写入50万条记录:
declare @i int
set @i=1
while @i<=500000
begin
insert into redheadedfile(filenames,senduser) values('我的分页算法','陆俊铭')
set @i=@i 1
end
GO
用Microsoft Visual Studio .NET 2003创建一张WebForm网页(本人起名webform8.aspx)
前台代码片段如下(webform8.aspx):
<%@ Page language="c#" Codebehind="WebForm8.aspx.cs" AutoEventWireup="false" Inherits="WebApplication6.WebForm8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm8</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datalist id="datalist1" AlternatingItemStyle-BackColor="#f3f3f3" Width="100%" CellSpacing="0"
CellPadding="0" Runat="server">
<ItemTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%"

align="center"><%#DataBinder.Eval(Container.DataItem,"filenames")%></td>
<td width="30%"

align="center"><%#DataBinder.Eval(Container.DataItem,"senduser")%></td>
<td width="30%"

align="center"><%#DataBinder.Eval(Container.DataItem,"id")%></td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
<div align="center">共<asp:label id="LPageCount" Runat="server" ForeColor="#ff0000"></asp:label>页/共

<asp:label id="LRecordCount" Runat="server" ForeColor="#ff0000"></asp:label>记录
<asp:linkbutton id="Fistpage" Runat="server"

CommandName="0">首页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;<asp:linkbutton id="Prevpage" Runat="server" CommandName="prev">

上一页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;<asp:linkbutton id="Nextpage" Runat="server"

CommandName="next">下一页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;<asp:linkbutton id="Lastpage" Runat="server"

CommandName="last">尾页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;当前第<asp:label id="LCurrentPage" Runat="server"

ForeColor="#ff0000"></asp:label>页&nbsp;&nbsp;&nbsp;&nbsp;跳页<asp:TextBox ID="gotoPage" Runat="server" Width="30px"

MaxLength="5" AutoPostBack="True"></asp:TextBox></div>
</form>
</body>
</HTML>
后台代码片段如下(webform8.aspx.cs)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication6
{
/// <summary>
/// WebForm8 的摘要说明。
/// </summary>
public class WebForm8 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton Fistpage;
protected System.Web.UI.WebControls.LinkButton Prevpage;
protected System.Web.UI.WebControls.LinkButton Nextpage;
protected System.Web.UI.WebControls.LinkButton Lastpage;
protected System.Web.UI.WebControls.DataList datalist1;
protected System.Web.UI.WebControls.DropDownList mydroplist;
protected System.Web.UI.WebControls.Label LPageCount;
protected System.Web.UI.WebControls.Label LRecordCount;
protected System.Web.UI.WebControls.Label LCurrentPage;
protected System.Web.UI.WebControls.TextBox gotoPage;
const int PageSize=20;//定义每页显示记录
int PageCount,RecCount,CurrentPage,Pages,JumpPage;//定义几个保存分页参数变量

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
RecCount = Calc();//通过Calc()函数获取总记录数
PageCount = RecCount/PageSize OverPage();//计算总页数(加上OverPage()函数防止有余数造成显示

数据不完整)

ViewState["PageCounts"] = RecCount/PageSize -

ModPage();//保存总页参数到ViewState(减去ModPage()函数防止SQL语句执行时溢出查询范围,可以用存储过程分页算法来理解这句)
ViewState["PageIndex"] = 0;//保存一个为0的页面索引值到ViewState
ViewState["JumpPages"] = PageCount;//保存PageCount到ViewState,跳页时判断用户输入数是否超出页

码范围
//显示LPageCount、LRecordCount的状态
LPageCount.Text = PageCount.ToString();

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:灵活正确的实现.NET插件机制

下一篇:在Asp.net中为图像加入版权信息