ASP.net中的Datagrid自定义分页功能

原创 2003年11月16日 10:39:00

看到在CSDN中已经有了不少自定义分页的例子,参考了很多,但是老觉得别人写的功能不是很完备,自己也摸索的写了一个,主要参考了“孟子E章”的分页例子。

这里使用的是VB.net语言。

DataGridPaging.aspx

 

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataGridPaging.aspx.vb" Inherits="Webwuye.DataGridPaging"%>  参考下面我的回复中,这里帖不上代码。 <?xml:namespace prefix = asp />

       

DataGridPaging.aspx.vb

Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI

Public Class DataGridPaging
    Inherits System.Web.UI.Page

    Protected WithEvents btnFirst As System.Web.UI.WebControls.LinkButton
    Protected WithEvents btnPrev As System.Web.UI.WebControls.LinkButton
    Protected WithEvents btnNext As System.Web.UI.WebControls.LinkButton
    Protected WithEvents btnLast As System.Web.UI.WebControls.LinkButton
    Protected WithEvents lblCurrentIndex As System.Web.UI.WebControls.Label
    Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label
    Protected WithEvents lblRegisterCount As System.Web.UI.WebControls.Label
    Protected WithEvents DataGrid As System.Web.UI.WebControls.DataGrid
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Dim cnn As New SqlConnection("Password=;  User ID=sa;Initial Catalog=northwind;Data Source=localhost;Connect Timeout=30")
    Dim adp As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet

    '   Dim intStartIndex As Long

    '定义全局静态变量,记录选中页面之前的总记录数
    Private Shared preceding As Long
    Private Shared gridPageSize As Int16

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        btnFirst.Text = "最首页"
        btnPrev.Text = "前一页"
        btnNext.Text = "下一页"
        btnLast.Text = "最后页"

        If Not IsPostBack Then
            Try
                Dim cmd As New SqlCommand("select count(*) from products", cnn)
                cnn.Open()
                '设置在使用自定义分页时 DataGrid 控件中的实际项数。
                'Dim i As Int16
                'i = cmd.ExecuteScalar()
                'MyDataGrid.VirtualItemCount=(i / MyDataGrid.PageSize)
                DataGrid1.VirtualItemCount = cmd.ExecuteScalar()
            Catch ex As Exception
                Response.Write("<script language=javascript>alert('错误原因:' + exp.tostring() )</script>")
            Finally
                cnn.Close()
            End Try
            preceding = DataGrid1.PageSize
            BindGrid()

        End If

    End Sub

    Sub BindGrid()
        '使用DataSet方法填充DataGrid
        Dim str As String = " * "
        'Dim gridPageSize As Int16 = DataGrid1.PageSize
        gridPageSize = DataGrid1.PageSize

        Dim cmdNumber As New SqlCommand("select count(*) from products ", cnn)

        Try
            cnn.Open()
            '设置在使用自定义分页时 DataGrid 控件中.VirtualItemCount存放总的实际项数。
            DataGrid1.VirtualItemCount = cmdNumber.ExecuteScalar()

            '使用求余保证最后一页的合理显示
            If preceding >= DataGrid1.VirtualItemCount Then
                gridPageSize = DataGrid1.VirtualItemCount Mod DataGrid1.PageSize
                If gridPageSize = 0 Then
                    gridPageSize = DataGrid1.PageSize
                End If
            End If

            str = "select " + str + " from( select top " & gridPageSize.ToString + str + " from( select  top " + CStr(preceding) + str + " from products where productID >0 order by productID ) as p order by productID desc )as p order by productID "

            Cmd.CommandText = str
            Cmd.Connection = cnn
            adp.SelectCommand = Cmd
            'adp = New SqlDataAdapter(str, cnn)
            adp.Fill(ds, "products")
            DataGrid1.DataSource = ds.Tables("products").DefaultView
            DataGrid1.DataBind()
        Catch ex As Exception
            Response.Write("错误原因:" + ex.ToString())
            'Throw ex
        Finally
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
        End Try

        ShowStats()
        HideButton()


    End Sub

    ' -------------------------------------------  下面是分页功能    -----------------------------------------------

    Sub DataGrid1_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
        '点击GRID某一页时发生
        'GridXQ.aspx中DataGrid 的 OnPageIndexChanged="DataGrid1_Page"

        preceding = (e.NewPageIndex + 1) * DataGrid1.PageSize
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        BindGrid()
        ShowStats()
        HideButton()
    End Sub

    Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        'used by external paging UI

        '页面上Grid下方 linkbutton 的 onclick="PagerButtonClick" CommandArgument="next/prev/last/……"
        Dim arg As String = sender.CommandArgument

        Select Case arg
            Case "next"
                If (DataGrid1.CurrentPageIndex <= (DataGrid1.PageCount - 1)) Then
                    DataGrid1.CurrentPageIndex += 1
                    If DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 Then
                        preceding = DataGrid1.VirtualItemCount
                    Else
                        preceding += gridPageSize
                    End If
                End If
            Case "prev"
                If (DataGrid1.CurrentPageIndex > 0) Then
                    If DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 Then
                        preceding = (DataGrid1.PageCount - 1) * DataGrid1.PageSize
                    Else
                        preceding -= gridPageSize
                    End If
                    DataGrid1.CurrentPageIndex -= 1
                End If
            Case "last"
                DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1)
                preceding = DataGrid1.VirtualItemCount
            Case Else
                'page number 因为在button.commandArgument设置为0
                DataGrid1.CurrentPageIndex = System.Convert.ToInt32(arg)
                preceding = DataGrid1.PageSize
        End Select

        If (DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1)) Then
            btnNext.Enabled = False
        Else
            btnNext.Enabled = True
        End If

        If (DataGrid1.CurrentPageIndex = 0) Then
            btnPrev.Enabled = False
        Else
            btnPrev.Enabled = True
        End If

        BindGrid()
        ShowStats()
        HideButton()
    End Sub

    Sub ShowStats()

        lblCurrentIndex.Text = "第 " + (DataGrid1.CurrentPageIndex + 1).ToString() + " 页"
        lblPageCount.Text = "总共 " + DataGrid1.PageCount.ToString() + " 页"
        lblRegisterCount.Text = "共有 " + DataGrid1.VirtualItemCount.ToString + " 条记录"
    End Sub

    Sub HideButton()
        If (DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1)) Then
            btnNext.Enabled = False
            btnLast.Enabled = False
        Else
            btnNext.Enabled = True
            btnLast.Enabled = True
        End If

        If (DataGrid1.CurrentPageIndex = 0) Then
            btnPrev.Enabled = False
            btnFirst.Enabled = False
        Else
            btnPrev.Enabled = True
            btnFirst.Enabled = True
        End If
    End Sub
End Class

 

 

ASP.NET中利用DataGrid的自定义分页功能

ASP.NET中利用DataGrid的自定义分页功能 ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页 ASP.Net中的DataGrid有内置分页功能, 但是它...
  • u014739775
  • u014739775
  • 2014年05月12日 11:09
  • 1081

easyui datagrid 不显示分页栏,可以自定义显示分页按钮

项目里遇到的需求是 :用户在页面查询的时候,后台要根据一个查询条件的值去判断是不是要显示分页,因为查询一页数据,做了更新操作,被当前用户锁定,对于其他用户是不可见的,每次查询未处理的数据不显示分页按钮...
  • a417074865
  • a417074865
  • 2016年04月25日 20:43
  • 4435

asp.net中ListView分页功能

.aspx代码
  • yayun0516
  • yayun0516
  • 2014年11月17日 15:52
  • 1582

ASP.NET 自定义分页按钮

继昨晚用存储过程实现之后,以前一直打算着写个自己定义的分页按钮终于
  • pandeng4639088
  • pandeng4639088
  • 2014年11月21日 01:28
  • 791

Asp.NET MVC X.PageList.MVC 分页详解以及自定义样式

最近在研究MVC,自己做了个小项目;其中用到了分页功能,在网上找了很多相关的第三方插件,最后选择了X.PageList.MVC,插件是开源的,有利于学习所以选择了它,这并不是说其它的分页插件不好,只是...
  • tuchangsheng801004
  • tuchangsheng801004
  • 2015年08月07日 16:20
  • 4112

ASP.NET分页存储过程自定义用户控件

网上有很多分页存储过程,但是基本上都是提供一个单纯的存储过程,没有具体的怎样去实现。最近做一个项目用户的数数据相当大(一百万以上 的数据),如果用.NET自带的分页基本上是跑不动了,不是提示超时就是死...
  • my98800
  • my98800
  • 2016年04月18日 08:42
  • 185

asp.net 自定义 分页

  • 2011年11月30日 11:50
  • 2KB
  • 下载

ASP.NET存储过程自定义分页详解

  • 2009年09月19日 12:32
  • 66KB
  • 下载

asp.net自定义简单分页

  • 2008年08月27日 21:39
  • 220KB
  • 下载

C# ASP.net自定义分页

  • 2007年12月17日 14:23
  • 104KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ASP.net中的Datagrid自定义分页功能
举报原因:
原因补充:

(最多只允许输入30个字)