• DataGrid快速分页


    cs:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    public partial class user_shiyan2 : System.Web.UI.Page
    {
         private static int intRecordCount;                      //记录总数
         private static int intPageCount;                        //总页数
         private static int intCurrentPageIndex;                 //当前页码
         private DataView dv;

         protected void Page_Load(object sender, EventArgs e)
         {
             if (!Page.IsPostBack)
             {
                 //将当前页面置为1,即显示第一页的记录
                 intCurrentPageIndex = 1;

                 //设置属性
                // DataGrid1.AllowPaging = "True";
                 DataGrid1.AllowPaging = true;
                // DataGrid1AllowCustomPaging = "True";
                 DataGrid1.AllowCustomPaging = true;

                 //绑定数据(Bind()是一个自定义方法)
                 this.Bind(intCurrentPageIndex);
             }

         }
        
    private void Bind(int intCurrentPageIndex)
    {
         string strConn = ConfigurationSettings.AppSettings["Str_sql"].ToString();
         //每次只取出一页(即10条)数据的SQL语句
         string strSql = "select top 10 * from attorn where attorn_id not in (select top " + ( intCurrentPageIndex - 1)*10 + " attorn_id from attorn)";

         if (TextBox1.Text != null && TextBox1.Text != "")
         {

             strSql = "select top 10 * from attorn where attorn_id not in (select top " + (intCurrentPageIndex - 1) * 10 + " attorn_id from attorn where [User_name] like '%" + TextBox1.Text + "%') and [User_name] like '%" + TextBox1.Text + "%'";

         }
          //得到总记录数的SQL语句
          string strSqlCount = "select count(*) from attorn ";
         if(TextBox1.Text != null && TextBox1.Text != "")
         {
             strSqlCount = "select count(*) from attorn where    [User_name] like '%" + TextBox1.Text + "%'";
         }
          //建立与数据的连接
          SqlConnection dbConnection = new SqlConnection(strConn);
          dbConnection.Open();
          //创建适配器的实例,并填充数据
           SqlDataAdapter dsAdapter = new SqlDataAdapter(strSql,dbConnection);
          DataSet ds = new DataSet();
          dsAdapter.Fill(ds);
          dv = ds.Tables[0].DefaultView;

          //取得总记录数

          SqlCommand cmd = new SqlCommand(strSqlCount,dbConnection);
          intRecordCount = (Int32) cmd.ExecuteScalar();
         
          //关闭连接
          dbConnection.Close();
            
         //计算总页数
         double dblRecordCount;
         dblRecordCount = System.Convert.ToDouble(intRecordCount);
         intPageCount = (Int32)Math.Ceiling(dblRecordCount / 10);

         Label1.Text = intCurrentPageIndex.ToString();
         Label2.Text = intPageCount.ToString();
         //绑定数据
         DataGrid1.DataSource = dv;
         DataGrid1.DataBind();
    }

         protected void LinkButton1_Click(object sender, EventArgs e)
         {
             intCurrentPageIndex = 1;
             //重新绑定数据
             this.Bind(intCurrentPageIndex);  
         }
         protected void LinkButton2_Click(object sender, EventArgs e)
         {
             intCurrentPageIndex = intCurrentPageIndex - 1;
             if (intCurrentPageIndex <= 0)
             {
                 intCurrentPageIndex = 1;
             }
             //重新绑定数据
             this.Bind(intCurrentPageIndex);  
         }
         protected void LinkButton3_Click(object sender, EventArgs e)
         {
             intCurrentPageIndex = intCurrentPageIndex + 1;
              if(intCurrentPageIndex > intPageCount)
              {
                  intCurrentPageIndex = intPageCount;
              }
              //重新绑定数据
    this.Bind(intCurrentPageIndex);  
         }
         protected void LinkButton4_Click(object sender, EventArgs e)
         {
             intCurrentPageIndex = intPageCount;
             //重新绑定数据
             this.Bind(intCurrentPageIndex);  
         }
         protected void Button1_Click(object sender, EventArgs e)
         {

             intCurrentPageIndex = 1;
             //重新绑定数据
             this.Bind(intCurrentPageIndex);  
         }
    }

    aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="shiyan2.aspx.cs" Inherits="user_shiyan2" %>

    <!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>
    </head>
    <body>
         <form id="form1" runat="server">
         <div>
             <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
             <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">LinkButton</asp:LinkButton>
             <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">LinkButton</asp:LinkButton>
             <asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click">LinkButton</asp:LinkButton>
             <asp:DataGrid ID="DataGrid1" runat="server">
             </asp:DataGrid>
             <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
             &nbsp; &nbsp;&nbsp;
             <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
         </form>
    </body>
    </html>

    分页算法太复杂,让人看不懂, 让人看不懂的算法都不是好算法. 这个最简单:
    select top @page*@pagesize * from @table where ID not in (select top (@page-1)* @pagesize ID from @table)
  • 相关阅读:
    OpenCV 机器学习之 支持向量机的使用方法实例
    Lua中调用C函数(lua-5.2.3)
    我的Hook学习笔记
    几种开源分词工具的比較
    利用JasperReport+iReport进行Web报表开发
    移动前端开发者必知必会:移动设备概述
    图表插件--jqplot交互演示样例
    算法之二叉树各种遍历
    repo的小结
    sqlite3经常使用命令&amp;语法
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175995.html
Copyright © 2020-2023  润新知