RSS订阅优然探索
你的位置:首页 » 技术文章 » 正文

Vb.Net自定义DataGird,DataList分页显示代码

选择字号: 超大 标准 发布时间:2008-9-11 10:56:31 | 作者:admin | 0个评论 | 人浏览

 '##################################################################
    '自定义DataGird分页显示代码
    'DataGird绑定的显示 SqlAll显示的数据 LitPage 分页显示 CPage当前分页
    'SqlAll() 1、全部数据的代码 2、当前的SQL 3、TName表名 4、主键
    '##################################################################
    Public Function DataGridPage(ByRef DataShow As DataGrid, ByVal SqlAll() As String, ByRef LitPage As Literal, ByVal CPage As Integer)
        Dim MyPage As Integer
        Dim StrQ, StrM, SqlA, STr, DataName As String
        Dim MSum, Apage, k As Integer
        Dim Ck, Cktmp, Cstep As Integer
        Dim Ad, Ed As String
        Dim Addr, pa As String
        Dim PSqlA, PSql, PTName, PID As String
        PSqlA = SqlAll(1)
        PSql = SqlAll(2)
        PTName = SqlAll(3)
        PID = SqlAll(4)
        '全部 当前 表名 主键
        '#############################################################
        Dim AllNum, PageSize, MPage As String '总数,分页数 当前页
        '################################
        PageSize = Session(SkySpage)
        If PageSize Is Nothing Then
            PageSize = 10
        End If
        DataShow.PageSize = PageSize
        Cstep = 5
        '############################
        MyPage = CPage
        AllNum = CInt(Me.XCRetReGStep(PSqlA))
        Apage = AllNum
        If Apage Mod PageSize = 0 Then
            Apage = CInt(Apage / PageSize)
        Else
            Apage = CInt(CInt(Apage \ PageSize) + 1)
        End If
        If Apage = 0 Then
            Apage = 1
        End If
        If CPage > Apage Then
            CPage = Apage
            MyPage = CPage
        End If
        '#####################################################################

        Addr = Request.Url.ToString
        Addr = JoinChar(Addr)
        pa = Addr
        Dim chr34 As String = Chr(34)
        For Ck = CPage - Cstep To CPage
            If Ck > 0 Then
                If Ck = CPage Then
                    Ad += "<a href=" + pa + Ck.ToString + "><font color='#FF0000'>[" + Ck.ToString + "]</Font></a>"
                Else
                    Ad += "<a href=" + pa + Ck.ToString + ">[" + Ck.ToString + "]</a>"
                End If

            End If
        Next

        For Ck = CPage + 1 To CPage + Cstep
            If Ck <= Apage Then
                Ed += "<a href=" + pa + Ck.ToString + ">[" + Ck.ToString + "]</a>"
            End If
        Next
        '#####################################
        Dim strTemp As String
        strTemp = "<select class='XcSelect' name='pagef' size='1' onchange=""javascript:window.location='" + pa + "'+this.options[this.selectedIndex].value;"">"
        Dim II As Integer
        For II = 1 To Apage
            strTemp = strTemp + "<option value='" + II.ToString + "'"
            If CInt(CPage) = CInt(II) Then strTemp = strTemp + " selected "
            strTemp = strTemp.ToString & ">" + "第" + II.ToString + "页" + "</option>"
        Next
        strTemp = strTemp & "</select>"
        '#####################################
        LitPage.Text = Ad + Ed + " 页数:[" + CPage.ToString + "/" + Apage.ToString + "] 转到:" + strTemp
        '全部页Apage,当前页CPage,跨度 Ck
        '#####################################################################
        Dim NewData As New DataSet
        ConnStr = XCSqlCon()
        Dim StrSql, Tname As String
        StrSql = PSql
        Tname = PTName
        Dim Conn As New OleDbConnection
        Dim Cmd As New OleDbCommand
        Conn.ConnectionString = ConnStr
        Conn.Open()
        Cmd.Connection = Conn
        Cmd.CommandText = StrSql
        Dim objadpt As New OleDbDataAdapter(Cmd)
        objadpt.Fill(NewData, (MyPage - 1) * PageSize, PageSize, Tname)
        Cmd.Dispose()
        Conn.Close()
        Conn.Dispose()
        '#####################################################
        DataShow.DataKeyField = PID
        DataShow.DataSource = NewData.Tables(PTName)
        DataShow.DataBind()
        '#####################################################
        NewData.Clear()
        NewData.Dispose()
    End Function
    '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    '##################################################################
    '自定义Set分页显示代码
    'Set绑定的显示 SqlAll显示的数据 LitPage 分页显示 CPage当前分页
    'SqlAll() 1、全部数据的代码 2、当前的SQL 3、TName表名 4、主键
    '##################################################################
    Public Function DataGSetPage(ByVal SqlAll() As String, ByRef LitPage As Literal, ByVal CPage As Integer) As DataTable
        Dim MyPage As Integer
        Dim StrQ, StrM, SqlA, STr, DataName As String
        Dim MSum, Apage, k As Integer
        Dim Ck, Cktmp, Cstep As Integer
        Dim Ad, Ed As String
        Dim Addr, pa As String
        Dim PSqlA, PSql, PTName, PID As String
        PSqlA = SqlAll(1)
        PSql = SqlAll(2)
        PTName = SqlAll(3)
        PID = SqlAll(4)
        '全部 当前 表名 主键
        '#############################################################
        Dim AllNum, PageSize, MPage As String '总数,分页数 当前页
        '################################
        PageSize = 8
        ' DataShow.PageSize = PageSize
        Cstep = 5
        '############################
        MyPage = CPage
        AllNum = CInt(Me.XCRetReGStep(PSqlA))
        Apage = AllNum
        If Apage Mod PageSize = 0 Then
            Apage = CInt(Apage / PageSize)
        Else
            Apage = CInt(CInt(Apage \ PageSize) + 1)
        End If
        If Apage = 0 Then
            Apage = 1
        End If
        If CPage > Apage Then
            CPage = Apage
            MyPage = CPage
        End If
        '#####################################################################

        Addr = Request.Url.ToString
        Addr = JoinChar(Addr)
        pa = Addr

        For Ck = CPage - Cstep To CPage
            If Ck > 0 Then
                If Ck = CPage Then
                    Ad += "<a href=" + pa + Ck.ToString + "><font color='#FF0000'>[" + Ck.ToString + "]</Font></a>"
                Else
                    Ad += "<a href=" + pa + Ck.ToString + ">[" + Ck.ToString + "]</a>"
                End If

            End If
        Next

        For Ck = CPage + 1 To CPage + Cstep
            If Ck <= Apage Then
                Ed += "<a href=" + pa + Ck.ToString + ">[" + Ck.ToString + "]</a>"
            End If
        Next
        '#####################################
        Dim strTemp As String
        strTemp = "<select class='XcSelect' name='pagef' size='1' onchange=""javascript:window.location='" + pa + "'+this.options[this.selectedIndex].value;"">"
        Dim II As Integer
        For II = 1 To Apage
            strTemp = strTemp + "<option value='" + II.ToString + "'"
            If CInt(CPage) = CInt(II) Then strTemp = strTemp + " selected "
            strTemp = strTemp.ToString & ">" + "第" + II.ToString + "页" + "</option>"
        Next
        strTemp = strTemp & "</select>"
        '#####################################
        LitPage.Text = Ad + Ed + " 页数:[" + CPage.ToString + "/" + Apage.ToString + "] 转到:" + strTemp
        '全部页Apage,当前页CPage,跨度 Ck
        '#####################################################################
        Dim NewData As New DataSet
        ConnStr = XCSqlCon()
        Dim StrSql, Tname As String
        StrSql = PSql
        Tname = PTName
        Dim Conn As New OleDbConnection
        Dim Cmd As New OleDbCommand
        Conn.ConnectionString = ConnStr
        Conn.Open()
        Cmd.Connection = Conn
        Cmd.CommandText = StrSql
        Dim objadpt As New OleDbDataAdapter(Cmd)
        objadpt.Fill(NewData, (MyPage - 1) * PageSize, PageSize, Tname)
        Cmd.Dispose()
        Conn.Close()
        Conn.Dispose()
        'htm = htm&("News=" + NewData.Tables(Tname).Rows.Count.ToString)
        DataGSetPage = NewData.Tables(Tname)
        '#####################################################
        'DataShow.DataKeyField = PID
        'DataShow.DataSource = NewData.Tables(PTName)
        'DataShow.DataBind()
        '#####################################################
        'NewData.Clear()
        ' NewData.Dispose()
    End Function
    Public Function JoinChar(ByVal strUrl As String) As String
        '##################3
        '###没有?号,直接返回
        Dim A, Pagec, Ax, Tx As String
        Pagec = PageCurrent
        Dim I, J, K As Integer
        A = strUrl.Trim("&").Trim("?").ToLower
        If A.LastIndexOf("?") <= 0 Then
            '###没有?
            A = A + "?" + Pagec + "="
        Else
            '##有?
            If A.IndexOf(Pagec) <= 0 Then
                '##没有Pagec
                A = A + "&" + Pagec + "="
            Else
                '##有pageC
                I = A.IndexOf(Pagec)
                J = A.Length
                '####################################
                '###&没有提取
                Ax = A.Substring(I, J - I)
                '####################################
                J = Ax.IndexOf("&")
                If J > 0 Then
                    '##后面还有参数
                    Tx = Ax.Substring(0, J + 1)
                    Ax = Ax.Replace(Tx, "")
                    A = A.Replace(Tx, "") + "&" + Pagec + "="
                Else
                    '后面没有了()
                    A = A.Replace(Ax, "") + Pagec + "="
                End If
            End If
        End If
        JoinChar = A
    End Function

标签:VB.Net  DataGird  DataList  

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。