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
VB.NET DATALIST 分页代码
最新推荐文章于 2021-03-27 03:09:52 发布