Jason
Jason

Reputation: 213

Repeater forgetting how many items to display when next button is clicked

I have a repeater list that displays results in sets of 15. When you click the next button it shows the next 15 and so on.

I have added some buttons that will then filter the display to show the results in sets of 10, 25, 50.

When you click these it does work but when you click the next button it resets the display value to 15.

Below is the chunk of script:

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

Upvotes: 0

Views: 457

Answers (1)

Mesh
Mesh

Reputation: 6442

You need to 'persist' the number of items to get.

There are a few ways to do this....

Re-factor PageSize into a property with its backing kept in the viewstate, remember to initialise with appropriate default values.

Change the ItemsGet sub to use the property instead.

My vb is rusty!

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

Upvotes: 1

Related Questions