大批量的数据分页

来源:互联网 发布:mac系统语言彻底更改 编辑:程序博客网 时间:2024/05/18 01:24
 <%@ Page language="c#" Codebehind="crpage.aspx.cs" AutoEventWireup="false" Inherits="page.crpage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>crpage</title>
  <meta content="Microsoft Visual Studio .NET 7.1" >
  <meta content="C#" >
  <meta content="JavaScript" >
  <meta content="http://schemas.microsoft.com/intellisense/ie5" >
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form method="post" runat="server">
   <FONT face="宋体">
    <asp:datagrid style="Z-INDEX: 101; LEFT: 152px; ; TOP: 136px"
     runat="server" AutoGenerateColumns="False" CellPadding="4" BackColor="White" BorderWidth="1px"
     BorderStyle="None" BorderColor="#3366CC" Width="472px">
     <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
     <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
     <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
     <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
     <Columns>
      <asp:BoundColumn DataField="id" HeaderText="ID"></asp:BoundColumn>
      <asp:BoundColumn DataField="bb" HeaderText="地址"></asp:BoundColumn>
      <asp:BoundColumn DataField="aa" HeaderText="联系人"></asp:BoundColumn>
     </Columns>
     <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
    </asp:datagrid><asp:linkbutton style="Z-INDEX: 107; LEFT: 240px; ; TOP: 104px" runat="server">上一页</asp:linkbutton><asp:label style="Z-INDEX: 102; LEFT: 392px; ; TOP: 48px"
     runat="server">0</asp:label><asp:label style="Z-INDEX: 103; LEFT: 304px; ; TOP: 48px"
     runat="server">1</asp:label><asp:textbox style="Z-INDEX: 104; LEFT: 224px; ; TOP: 40px"
     runat="server" Width="24px">10</asp:textbox><asp:linkbutton style="Z-INDEX: 105; LEFT: 304px; ; TOP: 104px" runat="server">下一页</asp:linkbutton><asp:linkbutton style="Z-INDEX: 106; LEFT: 488px; ; TOP: 56px"
     runat="server">LinkButton</asp:linkbutton><asp:linkbutton style="Z-INDEX: 108; LEFT: 192px; ; TOP: 104px" runat="server">首页</asp:linkbutton><asp:linkbutton style="Z-INDEX: 109; LEFT: 376px; ; TOP: 104px"
     runat="server">最后一页</asp:linkbutton>
    <asp:Label style="Z-INDEX: 111; LEFT: 640px; ; TOP: 80px" runat="server">Label</asp:Label></FONT></form>
 </body>
</HTML>

 

 

后台代码

 

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.Configuration;
using System.Data.Common;
using System.Data.SqlClient;

namespace page
{
 /// <summary>
 /// crpage 的摘要说明。
 /// </summary>
 public class crpage : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label lbPageCount;
  protected System.Web.UI.WebControls.Label lbCurPage;
  protected System.Web.UI.WebControls.TextBox txtPageSize;
  protected System.Web.UI.WebControls.LinkButton lbNext;
  protected System.Web.UI.WebControls.LinkButton LinkButton1;
  protected System.Web.UI.WebControls.LinkButton lbUp;
  protected System.Web.UI.WebControls.LinkButton lbstat;
  protected System.Web.UI.WebControls.LinkButton LinkButton3;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!this.IsPostBack)
   {
    TbDataBind(Convert.ToInt32(lbCurPage.Text));
   }
  }
  private void TbDataBind(int CurPage)
  {
   SqlConnection  conn=new SqlConnection(ConfigurationSettings.AppSettings["MM_CONNECTION_STRING_price"].ToString());
   
   string Sql2 = "select TOP "+  Convert.ToString( CurPage * Convert.ToInt32(txtPageSize.Text) ) +" * from asd order by [id] asc";
   string Sql = "select TOP "+ txtPageSize.Text +" kk.* from (" + Sql2 + ") as kk order by kk.[id] desc";
   lbPageCount.Text = Sql;
   SqlDataAdapter da=new SqlDataAdapter(Sql,conn);
   DataSet ds =new DataSet();
   try
   {
    da.Fill(ds,"testtable");
    DataGrid1.DataSource=ds.Tables["testtable"];
    DataGrid1.DataBind();

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

  }
  #endregion

  private void lbNext_Click(object sender, System.EventArgs e)
  {   
   int num;
   if(Convert.ToInt32(Label1.Text)/10!=0)
   {
        num=Convert.ToInt32(Label1.Text)/10+1;
               
   }
   else
   {
                num=Convert.ToInt32(Label1.Text)/10;
   }
   if(Convert.ToInt32(lbCurPage.Text)>=num)
   {
                    Response.Write("<script>alert('已经到达最后一页!')</script>");
   }
   else
   {
    lbCurPage.Text = Convert.ToString(Convert.ToInt32(lbCurPage.Text) + 1);
    TbDataBind(Convert.ToInt32(lbCurPage.Text));
   }

  }

  private void lbUp_Click(object sender, System.EventArgs e)
  {
   if(Convert.ToInt32(lbCurPage.Text)==1)
   {
                Response.Write("<script>alert('已经是首页!')</script>");
   }
   else
   {
    lbCurPage.Text = Convert.ToString(Convert.ToInt32(lbCurPage.Text ) - 1);
    TbDataBind(Convert.ToInt32(lbCurPage.Text));
   }

  }

  private void lbstat_Click(object sender, System.EventArgs e)
  {
   lbCurPage.Text = Convert.ToString(1);
   TbDataBind(Convert.ToInt32(lbCurPage.Text));
  }

  private void LinkButton3_Click(object sender, System.EventArgs e)
  { 
   SqlConnection  conn= new SqlConnection(ConfigurationSettings.AppSettings["MM_CONNECTION_STRING_price"].ToString());
            string mySelectQuery="select  count(id)as sumid from asd";
       SqlCommand cmd=new SqlCommand(mySelectQuery,conn); 
   conn.Open();
   int dr =Convert.ToInt32( cmd.ExecuteScalar());
   //SqlDataReader dr=cmd.ExecuteReader();
   //while(dr.Read())
    //dr.Read();
   //{
                Label1.Text=dr.ToString();
   //}
              //dr.Close();
   cmd.Dispose();
      conn.Close();
   if(Convert.ToInt32(Label1.Text)/10==0)
   {
    lbCurPage.Text=Convert.ToString(Convert.ToInt32(Label1.Text)/10);
   }
   else
   {
               lbCurPage.Text=Convert.ToString(Convert.ToInt32(Label1.Text)/10+1);
   }
   TbDataBind(Convert.ToInt32(lbCurPage.Text));
  }
 }
}