转发器忘记单击下一个按钮时要显示的项目数

时间:2012-09-28 13:30:58

标签: asp.net vb.net repeater

我有一个转发器列表,以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

1 个答案:

答案 0 :(得分: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