V8

博客园 首页 新随笔 联系 订阅 管理
 

创建表
create table tb2(
id int identity(1,1),
title nvarchar(20),
content nvarchar(20),
primary key(id)
)

添加50万条记录或更多(建议每次最多50万条,除非你的服务器配置很好)
declare @i int
set @i=1
while @i<=500000
begin
    insert into tb2(title,content)values(’fcx写的分页控件’,’请您试用!!!’)
    set @i=@i+1
end
GO

 

1.aspx

<%@ Page language="c#" Codebehind="1.aspx.cs" AutoEventWireup="false" Inherits="fcx3._1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>快速分页</title>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
   <asp:DataGrid id="FastPage" runat="server" Width="584px" Height="208px" AutoGenerateColumns="False" ShowHeader="True" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
   <Columns>
   <asp:TemplateColumn HeaderText="ID">
   <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"id")%>
   </ItemTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn HeaderText="TITLE">
   <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"title")%>
   </ItemTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn HeaderText="CONTENT">
   <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"content")%>
   </ItemTemplate>
   </asp:TemplateColumn>
   </Columns>
   </asp:DataGrid>
   <asp:Label id="ThisPageNo" runat="server"></asp:Label>
   <asp:HyperLink id="FirstPage" runat="server">首页</asp:HyperLink>
   <asp:HyperLink id="PrePage" runat="server">上一页</asp:HyperLink>
   <asp:HyperLink id="NextPage" runat="server">下一页</asp:HyperLink>
   <asp:HyperLink id="LastPage" runat="server">尾页</asp:HyperLink>
   <input id="PageNo" type="text" style="width:45px;text-align:center"><input name="goto" onclick="javascript:if(PageNo.value!=’’){location.href=’1.aspx?PageNo=’+PageNo.value};" type="button" value="go">
   <asp:Label id="StatLabel" runat="server"></asp:Label>
 </body>
</HTML>

1.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;

namespace fcx3
{
 /// <summary>
 /// _1 的摘要说明。
 /// </summary>
 public class _1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid FastPage;
  protected System.Web.UI.WebControls.Label ThisPageNo;
  protected System.Web.UI.WebControls.HyperLink FirstPage;
  protected System.Web.UI.WebControls.HyperLink PrePage;
  protected System.Web.UI.WebControls.HyperLink NextPage;
  protected System.Web.UI.WebControls.HyperLink LastPage;
  protected System.Web.UI.WebControls.Label StatLabel;
  private string strconn="server=(local);database=test1;uid=sa;pwd=;";
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   //Response.Write(strconn.ToString());
   show(strconn,"tb2","id",20);
  }


  private void show(string sqlstr,string TName,string indexstr,int PSize) //(sqlstr数据库连接字符,TName数据表名称,indexstr索引字段,PSize每页大小)
  {
   string TableName=TName;
   SqlConnection conn=new SqlConnection(sqlstr.ToString());
   conn.Open();
   if (Cache["ts"]==null)
   {
    SqlCommand cmd=new SqlCommand("select count(*) as recordnum from "+TableName,conn);
    SqlDataReader rd=cmd.ExecuteReader();
    rd.Read();
    Cache.Insert("ts",Convert.ToInt32(rd["recordnum"]),null,DateTime.Now.AddMinutes(1),TimeSpan.Zero);
    rd.Close();
   }
   int TotalSize=(int)Cache["ts"];

   int PageSize=PSize;
   int PageCount=TotalSize/PageSize+OverPage(TotalSize,PageSize);

   string PageNo="1";

   if(this.Context.Request.QueryString["PageNo"]!=null){PageNo=this.Context.Request.QueryString["PageNo"].ToString();}
   try
   {
    if(Convert.ToInt32(PageNo)>=1&&Convert.ToInt32(PageNo)<=PageCount)
    {}
    else
    {
     conn.Close();
     this.Context.Response.End();
    }
   }
   catch
   {
    conn.Close();
    this.Context.Response.End();
   }

   ThisPageNo.Text="第"+PageNo+"页";
   string PageName=this.Context.Request.CurrentExecutionFilePath;
   StatLabel.Text="("+PageSize.ToString()+"条/页,共"+PageCount.ToString()+"页,"+TotalSize.ToString()+"条)";
   if(Convert.ToInt32(PageNo)>1)
   {
    FirstPage.NavigateUrl=PageName+"?PageNo=1";
    PrePage.NavigateUrl=PageName+"?PageNo="+Convert.ToString(Convert.ToInt32(PageNo)-1);
   }
   if(Convert.ToInt32(PageNo)<PageCount)
   {
    NextPage.NavigateUrl=PageName+"?PageNo="+Convert.ToString(Convert.ToInt32(PageNo)+1);
    LastPage.NavigateUrl=PageName+"?PageNo="+PageCount.ToString();
   }
   string sql="select top "+PageSize.ToString()+" * from "+TableName+" where "+indexstr+" not in(select top "+Convert.ToString((Convert.ToInt32(PageNo)-1)*PageSize)+" "+indexstr+" from "+TableName+" order by "+indexstr+" desc) order by "+indexstr+" desc";
   try
   {
    SqlDataAdapter sda=new SqlDataAdapter(sql,conn);
    DataSet ds=new DataSet();
    sda.Fill(ds);
    FastPage.DataSource=ds;
    FastPage.DataBind();
   }
   catch{}
   conn.Close();
  }

  private int OverPage(int ts,int ps)
  {
   int page1=0;
   if (ts%ps!=0){page1=1;}
   return page1;
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

 }
}

posted on 2006-09-20 10:42  V8  阅读(309)  评论(0编辑  收藏  举报