0

我有一个以 15 个为一组显示结果的中继器列表。当您单击下一个按钮时,它会显示下一个 15 个,依此类推。

我添加了一些按钮,这些按钮将过滤显示以显示 10、25、50 组的结果。

当您单击这些时,它确实可以工作,但是当您单击下一个按钮时,它会将显示值重置为 15。

以下是脚本块:

Public Property CurrentPage() As Integer
    Get
        ' look for current page in ViewState
            Dim o As Object = Me.ViewState("_CurrentPage")


        If o Is Nothing Then
            Return 0
        Else
            ' default to showing the first page
            Return CInt(o)
        End If
    End Get

    Set
        Me.ViewState("_CurrentPage") = value
    End Set
    End Property

    Protected Sub ItemsGet()
        Dim pageSize As Integer = 15
        ItemsGet(pageSize)
    End Sub

    Private Sub ItemsGet(ByVal pageSize As Integer)
        ' Read sample item info from XML document into a DataSet

        ' Populate the repeater control with the Items DataSet
        Dim objPds As New PagedDataSource()

        Dim selectedCategory As String = ddlCategory.SelectedValue.ToString()
        Dim selectedCategoryIndex As Integer = ddlCategory.SelectedIndex

        Dim selectedCategoryMonth As String = ddlCategoryMonth.SelectedValue.ToString()
        Dim selectedCategoryMonthIndex As Integer = ddlCategoryMonth.SelectedIndex

        Dim query = GetXmlDataSet()

        If (Not String.IsNullOrEmpty(selectedCategory) And selectedCategoryIndex > 0) Then
            query = query.Where(Function(x) x("SCategoryName") = selectedCategory)
        End If
        If (Not String.IsNullOrEmpty(selectedCategoryMonth) And selectedCategoryMonthIndex > 0) Then
            query = query.Where(Function(x) x("SCategoryMonth") = selectedCategoryMonth)
        End If

        If (query.Count() > 0) Then

            objPds.DataSource = query.CopyToDataTable().Rows
            objPds.AllowPaging = True
            objPds.PageSize = pageSize

            objPds.CurrentPageIndex = CurrentPage

            lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objPds.PageCount.ToString()

            ' Disable Prev or Next buttons if necessary
            cmdPrev.Enabled = Not objPds.IsFirstPage
            cmdNext.Enabled = Not objPds.IsLastPage
            Display10.Enabled = True
            Display25.Enabled = True
            Display50.Enabled = True

            categories.DataSource = objPds
            categories.DataBind()
        Else
            CurrentPage = 0
            categories.Controls.Clear()
            cmdPrev.Enabled = False
            cmdNext.Enabled = False
            Display10.Enabled = False
            Display25.Enabled = False
            Display50.Enabled = False
            lblCurrentPage.Text = "Page: 0 of 0 "
        End If
    End Sub

    Private Sub Display10_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim pageSize As Integer = 10
        CurrentPage = 0
        ItemsGet(pageSize)
    End Sub

    Private Sub Display25_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim pageSize As Integer = 25
        CurrentPage = 0
        ItemsGet(pageSize)
    End Sub

    Private Sub Display50_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim pageSize As Integer = 50
        CurrentPage = 0
        ItemsGet(pageSize)
    End Sub



    Private Sub cmdPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Set viewstate variable to the previous page
        CurrentPage -= 1

        ' Reload control
        ItemsGet()
    End Sub

    Private Sub cmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Set viewstate variable to the next page
        CurrentPage += 1

        ' Reload control
        ItemsGet()
    End Sub


    Protected Sub ddlCategory_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
        CurrentPage = 0
        ItemsGet()
    End Sub

    Protected Sub ddlCategoryMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        CurrentPage = 0
        ItemsGet()
    End Sub
4

1 回答 1

1

您需要“坚持”要获取的项目数量。

有几种方法可以做到这一点......

将 PageSize 重构为一个属性,其支持保留在视图状态中,记住使用适当的默认值进行初始化。

更改 ItemsGet 子以改用该属性。

我的vb生锈了!

Public Property PageSize() As Integer
    Get
       If Me.ViewState("PageSize") Is Nothing Then
            Me.ViewState("PageSize") = 15
        End If

        Return CInt( Me.ViewState("PageSize") )      
    End Get
    Set
        Me.ViewState("PageSize") = value
    End Set
 End Property
于 2012-09-28T13:42:33.610 回答