Listview advances 2 pages at a time instead of one as set in timer control

199 Views Asked by At

I have a Listview control with a DataPager and a timer to auto advance a single page of the Listview every one second.

The problem is that the Listview jumps 2 pages....

Protected Sub timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick

    'Verify that the session variable is not null
    If Session("startRowIndex") Is Nothing Then
        Session.Add("startRowIndex", 0)
    End If

    'Create a variable to store the first record to show
    Dim startRowIndex As Integer = Convert.ToInt32(Session("startRowIndex"))

    'Increase the first record to display in the size of the page
    startRowIndex = startRowIndex + Me.DataPager1.MaximumRows

    'Show from the first record to the size of the page
    Me.DataPager1.SetPageProperties(startRowIndex, Me.DataPager1.MaximumRows, True)

    'If the first record exceeds the total number of records, restart the count.
    If startRowIndex > Me.DataPager1.TotalRowCount Then
        startRowIndex = 0
    End If

    Session("startRowIndex") = startRowIndex
End Sub

The DataPager control...

 <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="1" >
    <Fields>
        <asp:NextPreviousPagerField ButtonType="Button" 
             ShowFirstPageButton="True" 
             ShowNextPageButton="False"
             ShowPreviousPageButton="True" />
        <asp:NumericPagerField ButtonCount="10" />
        <asp:NextPreviousPagerField ButtonType="Button" 
             ShowLastPageButton="True" 
             ShowNextPageButton="true"
             ShowPreviousPageButton="False" />
    </Fields>
</asp:DataPager>

The time control...

<asp Timer ID="timer" runat="server" Interval="1000" OnTick="timer_Tick">
</asp Timer>

Am I missing something simple here?

1

There are 1 best solutions below

1
On

You are increasing strartrowindex before setting page properties, which is wrong. It should be:

*EDIT : * Remember, RowIndex is 0 based and count is always LastRowIndex + 1. You need to deduct 1 from MaximumRows.

'Show from the first record to the size of the page
Me.DataPager1.SetPageProperties(startRowIndex, Me.DataPager1.MaximumRows, True)

'Increase the first record to display in the size of the page
startRowIndex = startRowIndex + Me.DataPager1.MaximumRows -1