带有分页的VB.Net GridView表,在PageIndexChanging事件中再次显示任何内容或第一页

时间:2015-11-04 18:19:17

标签: asp.net .net database vb.net gridview

我正在尝试在网格视图中显示从Oracle查询的数据,并添加了带有PageIndexChanging事件的分页。但是,经过几次不同的尝试进行小的更改后,点击另一个页面要么显示没有网格视图的屏幕,要么仅在第1页上刷新带有gridview的网站。之前从未使用过网络应用程序/网站,有什么想法吗?

代码:

Imports System.Data

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
    Dim con As OleDb.OleDbConnection
    Dim command As OleDb.OleDbCommand
    Dim commandstr As String
    Dim wherestr As String
    Dim dt As DataTable = New DataTable
    Dim oda As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter

    'Remove any non-aplhanumeric characters from the input string
    MeterID_tb.Text = Regex.Replace(MeterID_tb.Text, "[^A-Za-z0-9]+", "")
    'Opco_tb.Text = Regex.Replace(Opco_tb.Text, "[^A-Za-z0-9]+", "")
    DateFrom_tb.Text = Regex.Replace(DateFrom_tb.Text, "[^A-Za-z0-9]+", "")
    DateTo_tb.Text = Regex.Replace(DateTo_tb.Text, "[^A-Za-z0-9]+", "")

    'Don't allow the user to search without a filter. The results returned will be too large
    'Opco_tb.Text = "" &
    If (MeterID_tb.Text = "") And (Division_db.SelectedValue = "Any") And (DateFrom_tb.Text = "") And (DateTo_tb.Text = "") Then

        Dim strScript As String = "<script language=JavaScript>"
        strScript += "alert(""" & "You must enter at least one search parameter." & """);"
        strScript += "</script>"

        If (Not Page.IsStartupScriptRegistered("clientScript")) Then
            Page.RegisterStartupScript("clientScript", strScript)
        End If

        Exit Sub
    End If

    con = New OleDb.OleDbConnection(*Hidden*)
    commandstr = "SELECT METERID as ""Meter ID"", REPID as ""Rep ID"", DIVISION as ""Division"", CITY as ""City"", ADDRESS as ""Address"", RATECODE as ""Ratecode"", METERFORM as ""Meter Form"", METERSTATUS as ""Meter Status"", METERPOINTSTATUS as ""Meter Point Status"", BILLINGCYCLE as ""Billing Cycle"", FILENAME as ""Filename"", FILEDATETIME as ""File Date/Time"" from BAD_METER_LIST"
    wherestr = " WHERE"

    If MeterID_tb.Text <> "" Then
        If wherestr = " WHERE" Then
            wherestr = wherestr + " METERID = '" + MeterID_tb.Text + "'"
        Else
            wherestr = wherestr + " AND METERID = '" + MeterID_tb.Text + "'"
        End If
    End If

    If Division_db.SelectedValue <> "Any" Then
        If wherestr = " WHERE" Then
            wherestr = wherestr + " DIVISION = " + Division_db.SelectedValue
        Else
            wherestr = wherestr + " AND DIVISION = " + Division_db.SelectedValue
        End If
    End If

    If DateFrom_tb.Text <> "" Then

        If wherestr = " WHERE" Then
            wherestr = wherestr + " FILEDATETIME >= to_date('" & DateFrom_tb.Text & "', 'mmddyyyy')"
        Else
            wherestr = wherestr + " AND FILEDATETIME >= to_date('" & DateFrom_tb.Text & "', 'mmddyyyy')"
        End If
    End If

    If DateTo_tb.Text <> "" Then

        If wherestr = " WHERE" Then
            wherestr = wherestr + " FILEDATETIME <= to_date('" & DateTo_tb.Text & "', 'mmddyyyy')"
        Else
            wherestr = wherestr + " AND FILEDATETIME <= to_date('" & DateTo_tb.Text & "', 'mmddyyyy')"
        End If
    End If

    command = New OleDb.OleDbCommand(commandstr + wherestr)
    command.Connection = con
    con.Open()
    oda.SelectCommand = command
    oda.Fill(dt)

    Me.Grid_Bad_Meters.DataSource = dt
    Me.Grid_Bad_Meters.DataBind()

End Sub

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

End Sub

Protected Sub Grid_Bad_Meters_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles Grid_Bad_Meters.PageIndexChanging
    'Grid_Bad_Meters.Visible = True
    Grid_Bad_Meters.PageIndex = e.NewPageIndex
    Grid_Bad_Meters.DataBind()


End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您需要在更改GridView时重新绑定PageIndex

Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page

#Region "Subs"

    Private Sub CleanInput()
        'Remove any non-aplhanumeric characters from the input string
        MeterID_tb.Text = Regex.Replace(MeterID_tb.Text, "[^A-Za-z0-9]+", "")
        'Opco_tb.Text = Regex.Replace(Opco_tb.Text, "[^A-Za-z0-9]+", "")
        DateFrom_tb.Text = Regex.Replace(DateFrom_tb.Text, "[^A-Za-z0-9]+", "")
        DateTo_tb.Text = Regex.Replace(DateTo_tb.Text, "[^A-Za-z0-9]+", "")
    End Sub

    Private Sub RequireFilter
        'Don't allow the user to search without a filter. The results returned will be too large
        'Opco_tb.Text = "" &
        If ((MeterID_tb.Text = "") 
             AndAlso (Division_db.SelectedValue = "Any") 
             AndAlso (DateFrom_tb.Text = "") 
             AndAlso (DateTo_tb.Text = "")) Then
            Dim strScript As String = "<script language=JavaScript>"
            strScript += "alert(""" & "You must enter at least one search parameter." & """);"
            strScript += "</script>"

            If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                Page.RegisterStartupScript("clientScript", strScript)
            End If

            Exit Sub
        End If
    End Sub

    Private Sub FillGridView
        Dim con As OleDb.OleDbConnection
        Dim command As OleDb.OleDbCommand
        Dim commandstr As String
        Dim wherestr As String
        Dim dt As DataTable = New DataTable
        Dim oda As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter


        con = New OleDb.OleDbConnection(*Hidden*)
        commandstr = "SELECT METERID as ""Meter ID"", REPID as ""Rep ID"", DIVISION as ""Division"", CITY as ""City"", ADDRESS as ""Address"", RATECODE as ""Ratecode"", METERFORM as ""Meter Form"", METERSTATUS as ""Meter Status"", METERPOINTSTATUS as ""Meter Point Status"", BILLINGCYCLE as ""Billing Cycle"", FILENAME as ""Filename"", FILEDATETIME as ""File Date/Time"" from BAD_METER_LIST"
        wherestr = " WHERE"

        If MeterID_tb.Text <> "" Then
            If wherestr = " WHERE" Then
                wherestr = wherestr + " METERID = '" + MeterID_tb.Text + "'"
            Else
                wherestr = wherestr + " AND METERID = '" + MeterID_tb.Text + "'"
            End If
        End If

        If Division_db.SelectedValue <> "Any" Then
            If wherestr = " WHERE" Then
                wherestr = wherestr + " DIVISION = " + Division_db.SelectedValue
            Else
                wherestr = wherestr + " AND DIVISION = " + Division_db.SelectedValue
            End If
        End If

        If DateFrom_tb.Text <> "" Then

            If wherestr = " WHERE" Then
                wherestr = wherestr + " FILEDATETIME >= to_date('" & DateFrom_tb.Text & "', 'mmddyyyy')"
            Else
                wherestr = wherestr + " AND FILEDATETIME >= to_date('" & DateFrom_tb.Text & "', 'mmddyyyy')"
            End If
        End If

        If DateTo_tb.Text <> "" Then

            If wherestr = " WHERE" Then
                wherestr = wherestr + " FILEDATETIME <= to_date('" & DateTo_tb.Text & "', 'mmddyyyy')"
            Else
                wherestr = wherestr + " AND FILEDATETIME <= to_date('" & DateTo_tb.Text & "', 'mmddyyyy')"
            End If
        End If

        command = New OleDb.OleDbCommand(commandstr + wherestr)
        command.Connection = con
        con.Open()
        oda.SelectCommand = command
        oda.Fill(dt)

        Me.Grid_Bad_Meters.DataSource = dt
        Me.Grid_Bad_Meters.DataBind()
    End Sub

#End Region

    Protected Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
        CleanInput()
        RequireFilter()
        FillGridView()
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

    Protected Sub Grid_Bad_Meters_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles Grid_Bad_Meters.PageIndexChanging
        'Grid_Bad_Meters.Visible = True
        Grid_Bad_Meters.PageIndex = e.NewPageIndex
        Grid_Bad_Meters.DataBind()
        FillGridView()
    End Sub
End Class