Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString)
    Dim cmd As SqlCommand
    Dim ds As New DataSet
    Dim ad As SqlDataAdapter

    'DECLARE GLOBLE VARIABLE WHICH HOLDS CURRENT PAGE INDEX.
    Dim currentIndex As Integer = 0
    Dim pages As New PagedDataSource 'INITIALIZE THE PAGE VARIABLE.
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            bind()
        End If

    End Sub
    Protected Sub bind()
        'FETCH DATA FROM SQL DATABASE.
        cmd = New SqlCommand("select * from tb", con)
        ad = New SqlDataAdapter(cmd)
        ad.Fill(ds)

        'ALLOW PAGING
        pages.AllowPaging = True
        'DISPLAY NUMBER OF DATA PER PAGE 
        pages.PageSize = 5

        'CONFIGURE DATA SOURCE TO PAGEDATASOURCE.
        pages.DataSource = ds.Tables(0).DefaultView

        'SET CURRENT PAGE INDEX RETRIEVED BY VIEWSTATE.
        pages.CurrentPageIndex = ViewState("currentval")

        'NOW SIMPLY PASS ALL PAGED VARIABLE VALUE TO DATALIST CONTROL.
        DataList1.DataSource = pages    'TYPE YOUR DATA CONTROL NAME LIKE LISTVIEW,REPEATER ETC.
        DataList1.DataSourceID = ""
        DataList1.DataBind()
        DataList1.DataKeyField = "num"

        'RETAINED THE ABOVE CURRENTINDEX VALUES WHICH I STORE IN VIEWSTATE. YOU CAN STORE IN SESSION AT YOUR OWNRISK.
        ViewState("currentval") = currentIndex


    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'SET THE INDEX OF DATALIST TO 0 .MEANS FIRST PAGE OF PAGEDDATASOURCE.
        currentIndex = 0
        'store CURRENTVAL VARIABLE TO VIEWSTATE FOR RETAINING VALUE.
        ViewState("currentval") = currentIndex
        'CALL BIND METHOD FOR BINDING DATALIST CONTROL.
        bind()




    End Sub
    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'I MANUALLY CHECK WETHER NEXT BUTTON REACH LAST RECORD OR NOT.
        If Not ViewState("currentval") >= 11 Then
            'INCRESE VALUE OF GLOBLE VARIABL TO SEE NEXT PAGE DATA.
            currentIndex += ViewState("currentval") + 1
            'STORE THE CURRENTVAL VALUE TO VIEWSTATE.
            ViewState("currentval") = currentIndex
            'CALL BIND METHOD TO BIND CONTROL DATA.
            bind()
        Else
            'MEANS YOU ARE IN LAST PAGE.
            Response.Write("YOU ARE ALREADY IN LAST PAGE...")
        End If


    End Sub
    Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        'I MANUALLY CHECK WETHER NEXT BUTTON REACH FIRST RECORD OR NOT.
        If ViewState("currentval") > 0 Then
            'DECREMENT THE GLOBLE VARIABLE (currentIndex) TO 1 .FOR VIEWING PREVIOUS RECORD.
            currentIndex = ViewState("currentval") - 1
            'STORE THE NEW VALUE TO VIEWSTATE .
            ViewState("currentval") = currentIndex
            'CALL CUSTOM BIND METHOD. .
            bind()
        Else
            'MEANS YOU ARE IN FIRST PAGE.
            Response.Write("YOU ARE ALREADY IN FIRST PAGE...")
        End If





    End Sub
    Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        'I MENUALLY CALCULATE THE NUMBER . BECAUSE THERE IS NO METHOD FOR GO TO LAST PAGE.
        currentIndex = 11
        'STORE THE CURRENTINDEX VALUE TO VIEWSTATE.
        ViewState("currentval") = currentIndex
        'CALL CUSTOM BIND METHOD.
        bind()

    End Sub
End Class
