您的位置:首页 > 编程语言 > VB

VB.NET DATALIST 分页代码

2020-03-01 19:38 447 查看

Imports System.Data
Imports System.Data.OleDb
Public Class _NewsModify
    Inherits System.Web.UI.Page
    Dim currentPage, pageCount, pageSize As Integer
    Function count() As Integer
        Dim connstr As String = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("../") + "App_Data/data.mdb"
        Dim conn As OleDbConnection = New OleDbConnection(connstr)
        Dim sql As String = "select count(n_id) from news"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        count = cmd.ExecuteScalar()
        conn.Close()
    End Function
    Function dataView() As DataView
        pageSize = CInt(itemno.SelectedItem.Text)
        currentPage = ViewState("currentPage")
        Dim startIndex As Integer
        startIndex = currentPage * pageSize
        Dim connstr As String = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("../") + "App_Data/data.mdb"
        Dim conn As OleDbConnection = New OleDbConnection(connstr)
        Dim newssql As String
        If order.SelectedItem.Text = "新" Then
            newssql = "select n_title from news order by n_id desc"
        Else
            newssql = "select n_title from news"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(newssql, conn)
        Dim adp As OleDbDataAdapter = New OleDbDataAdapter()
        adp.SelectCommand = cmd
        Dim ds As DataSet = New DataSet()
        adp.Fill(ds, startIndex, pageSize, "news")
        Dim bd As OleDbCommandBuilder = New OleDbCommandBuilder(adp)
        Dim dv As DataView = New DataView(ds.Tables("news"))
        dataView = dv
    End Function
    Sub page_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
        currentPage = ViewState("currentPage")
        pageCount = ViewState("pageCount")
        Dim cmd As String = e.CommandName
        Select Case cmd
            Case "Next"
                If currentPage < pageCount - 1 Then
                    currentPage = currentPage + 1
                End If
            Case "Prev"
                If currentPage > 0 Then
                    currentPage = currentPage - 1
                End If
        End Select
        ViewState("currentPage") = currentPage
        bindList()
    End Sub
    Sub bindList()
        datalist1.DataSource = dataView()
        datalist1.DataBind()
        lbtNext.Enabled = True
        lbtPrev.Enabled = True
        pageCount = ViewState("pageCount")
        If currentPage = pageCount - 1 Then
            lbtNext.Enabled = False
        End If
        If currentPage = 0 Then
            lbtPrev.Enabled = False
        End If
        lblcurrentPage.Text = "第" + (currentPage + 1).ToString() + "页"
    End Sub
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        pageSize = CInt(itemno.SelectedItem.Text)
        bindList()
        pageCount = count() / pageSize
        ViewState("pageCount") = pageCount
        lblpageCount.Text = "共 " + pageCount.ToString() + " 页"
        If Not IsPostBack Then
            currentPage = 0
            ViewState("currentPage") = 0
            lblNewsCount.Text = count().ToString() + " 条新闻"
        End If
    End Sub
End Class

  • 点赞
  • 收藏
  • 分享
  • 文章举报
VBNETOR 发布了2 篇原创文章 · 获赞 0 · 访问量 1261 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: