求根据条件查询后绑定Gridview,并自定义分页的示例代码??
多条件查询后,将结果绑定到Gridview上,并且自定义Gridview的分页,在分页的时候能够显示正常,而不是点击页数的时候初始化了。
希望是实例代码,能够承受百万级数据

解决方案 »

  1.   

    根据每页的行数,去数据库查询相应的条数比如每页10条,然后在第二页时去数据库取第11~20条数据Select top 10 * from TableName where Id not in (Select top 10 Id from TableName)
      

  2.   

    分页用aspnetpager 官方网站上有例子/// Handles the Load event of the Page control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
            protected void Page_Load(object sender, EventArgs e)
            {
                //UART.Common.CheckRoleAndPermission.CheckLogin();
                //UART.Common.CheckRoleAndPermission.Check(0, 10);
                //第一次加载页面 设置分页控件总数 绑定数据
                if (!this.IsPostBack)
                {
                    UART.BLL.Sales.SalesOrderHeader bll = new UART.BLL.Sales.SalesOrderHeader();
                    this.lblStrWhere.Text = string.Empty;
                    this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                    this.BindData();
                    
                }
            }
    /// <summary>
            /// 绑定订单列表
            /// </summary>
            private void BindData()
            {
                UART.BLL.Sales.SalesOrderHeader bll = new UART.BLL.Sales.SalesOrderHeader();
                DataSet ds = bll.GetList(this.AspNetPager.PageSize, this.AspNetPager.CurrentPageIndex, this.lblStrWhere.Text, "1");
                this.myGridView.DataSource = ds;
                this.myGridView.DataBind();
            }        /// <summary>
            /// 分页绑定数据
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void AspNetPager_PageChanged(object sender, EventArgs e)
            {
                this.BindData();
            }/// <summary>
            /// Handles the Click event of the btnSearch control.查询相关订单信息
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
            protected void btnSearch_Click(object sender, EventArgs e)
            {
                this.lblStrWhere.Text = "";
                //设置查询条件
                  StringBuilder strWhere = new StringBuilder();
                if (this.txtCustomerName.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and CustomerName like '%{0}%'", UART.Common.PageValidate.InputText(this.txtCustomerName.Text, 50));
                }
                if (this.txtPhone.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and Phone like '%{0}%'", UART.Common.PageValidate.InputText(this.txtPhone.Text, 20));
                }
                if (this.txtCellPhone.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and CellPhone like '%{0}%'", UART.Common.PageValidate.InputText(this.txtCellPhone.Text, 20));
                }
                if (this.txtAddress.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and Address like '%{0}%'", UART.Common.PageValidate.InputText(this.txtAddress.Text, 100));
                }
                if (strWhere.Length > 0)
                {
                    this.lblStrWhere.Text = strWhere.ToString().Substring(5, strWhere.Length - 5);
                }
                UART.BLL.Sales.SalesOrderHeader bll = new UART.BLL.Sales.SalesOrderHeader();
                this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                this.BindData();
            }
      

  3.   


    var q=db.Users.Skip(当前页数).Take(当前页数*pageSize)
      

  4.   

    分页存储过程USE [UART]
    GO
    /****** 对象:  StoredProcedure [dbo].[GetRecordFromPage]    脚本日期: 08/14/2010 00:45:30 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    /****** Object:  Stored Procedure dbo.GetRecordFromPage    Script Date: 2008-1-9 9:24:03 ******/-- =============================================
    /*
      函数名称: GetRecordFromPage
      函数功能: 获取指定页的数据
      参数说明: @tblName      包含数据的表名
               @fldName      关键字段名
               @PageSize     每页记录数
               @PageIndex    要获取的页码
               @OrderType    排序类型, 0 - 升序, 1 - 降序
               @strWhere     查询条件 (注意: 不要加 where)
      作  者: 铁拳
      邮  箱: [email protected]
      创建时间: 2004-07-04
      修改时间: 2004-07-04
    */
    -- =============================================
    ALTER PROCEDURE [dbo].[GetRecordFromPage]
        @tblName      varchar(255),       -- 表名
        @fldName      varchar(255),       -- 字段名
        @PageSize     int = 10,           -- 页尺寸
        @PageIndex    int = 1,            -- 页码
    @IsReCount    bit = 0,            -- 返回记录总数, 非 0 值则返回
        @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
        @strWhere     varchar(2000) = ''  -- 查询条件 (注意: 不要加 where)
    ASdeclare @strSQL   varchar(6000)       -- 主语句
    declare @strTmp   varchar(1000)       -- 临时变量
    declare @strOrder varchar(500)        -- 排序类型if @OrderType != 0
    begin
        set @strTmp = '<(select min'
        set @strOrder = ' order by [' + @fldName + '] desc'
    end
    else
    begin
        set @strTmp = '>(select max'
        set @strOrder = ' order by [' + @fldName +'] asc'
    endset @strSQL = 'select top ' + str(@PageSize) + ' * from ['
        + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
        + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
        + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
        + @strOrderif @strWhere != ''
        set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
            + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
            + @fldName + '] from [' + @tblName + '] where (' + @strWhere + ') '
            + @strOrder + ') as tblTmp) and (' + @strWhere + ') ' + @strOrderif @PageIndex = 1
    begin
        set @strTmp = ''
        if @strWhere != ''
            set @strTmp = ' where (' + @strWhere + ')'    set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + ']' + @strTmp + ' ' + @strOrder
    endif @IsReCount != 0
        set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhereexec (@strSQL)
      

  5.   

    【最好是Linq的查询分页代码】
      

  6.   


    LINQ+GridView+AspNetPager 实现分页 http://blog.csdn.net/peter521/archive/2008/08/06/2776879.aspx
      

  7.   

    另外可以:<PagerTemplate><div style="text-align:center; color:Blue">
          <asp:LinkButton ID="cmdFirstPage" runat="server" CommandName="Page" CommandArgument="First"
           Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">首页</asp:LinkButton>
          <asp:LinkButton ID="cmdPreview" runat="server" CommandArgument="Prev" CommandName="Page"
           Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">前页</asp:LinkButton>
          第<asp:Label ID="lblcurPage" ForeColor="Blue" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1  %>'></asp:Label>页/共<asp:Label
           ID="lblPageCount" ForeColor="blue" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></asp:Label>页
          <asp:LinkButton ID="cmdNext" runat="server" CommandName="Page" CommandArgument="Next"
           Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">后页</asp:LinkButton>
          <asp:LinkButton ID="cmdLastPage" runat="server" CommandArgument="Last" CommandName="Page"
           Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">尾页</asp:LinkButton>
           &nbsp;<asp:TextBox ID="txtGoPage" OnTextChanged="txtGoPage_TextChanged" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1 %>'
           Width="32px"  CssClass="inputmini"></asp:TextBox>页<asp:Button ID="Button3" runat="server"
               OnClick="Turn_Click" Text="转到" /></div>
    </PagerTemplate>---------------
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           GridView1.PageIndex = e.NewPageIndex;
            bind();//对GridView进行再次绑定,该函数由自己编写
        }
       protected void Turn_Click(object sender, EventArgs e)
        {        GridView1.PageIndex = int.Parse(((TextBox)GridView1.BottomPagerRow.FindControl("txtGoPage")).Text)-1;
            bind();//对GridView进行再次绑定,该函数由自己编写
        }
      

  8.   

    aspnetpager分页控件
    分页存储过程
    string sql="";
    sql+=条件?"":"and 字段=''";
    http://topic.csdn.net/u/20091204/21/722689e1-7824-497c-b709-4b1118264633.html
      

  9.   

    还有以下第三方控件,像aspnertpager pager..
      

  10.   

    http://www.cnblogs.com/wf225/archive/2007/08/10/850218.html
    GridView 实现自定义分页、排序、查询、添加、编辑、多选删除
    linq分页用var q=db.Users.Skip(当前页数).Take(当前页数*pageSize)