sample code for pagination needed urgently in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    sample code for pagination needed urgently in c#

    hi everybody,
    as in ( the scripts) page below, i want to do some pagination in c#.
    for example : <<previous>> 1 2 3 4 5 . . . . . 51 52 53 <<next>>
    help me plz..

    i have build htmltable using c# just like

    Response.Write( "<table">);
    ..........
    ..........
    ..........
    Response.Write( "</table">);

    i want display only 10 records in a table, and also i know total number of records help me plz.
  • radcaesar
    Recognized Expert Contributor
    • Sep 2006
    • 759

    #2
    Hi,
    Here is a sample paging code in ASP 3.0, Conver the syntax to c#.

    Why did u use HTML Table instead of DataGrid. Any particular reason ?

    :)


    <%
    Option Explicit

    Const adOpenStatic = 3
    Const adLockReadOnly = 1
    Const adCmdText = &H0001

    Dim CONN_STRING
    CONN_STRING = "Provider=Micro soft.Jet.OLEDB. 4.0; Data Source=" & Server.MapPath( "database.m db")

    Dim iPageSize
    Dim iPageCount
    Dim iPageCurrent
    Dim strOrderBy
    Dim strSQL
    Dim objPagingConn
    Dim objPagingRS
    Dim iRecordsShown
    Dim iFieldCount
    Dim iRecordCount
    Dim LoopRecordCount
    Dim pageNum
    Dim counter
    Dim markShowPage
    Dim I, J

    iPageSize = 6

    If Request.QuerySt ring("page") = "" Then
    iPageCurrent = 1
    Else
    iPageCurrent = CInt(Request.Qu eryString("page "))
    End If

    strSQL = "SELECT * from sample order by ID asc;"

    Set objPagingConn = Server.CreateOb ject("ADODB.Con nection")
    objPagingConn.O pen CONN_STRING

    Set objPagingRS = Server.CreateOb ject("ADODB.Rec ordset")
    objPagingRS.Pag eSize = iPageSize
    objPagingRS.Cac heSize = iPageSize
    objPagingRS.Ope n strSQL, objPagingConn, adOpenStatic, adLockReadOnly, adCmdText

    iPageCount = objPagingRS.Pag eCount
    iFieldCount = objPagingRS.Fie lds.Count
    iRecordCount = objPagingRS.Rec ordCount

    If iPageCurrent > iPageCount Then iPageCurrent = iPageCount
    If iPageCurrent < 1 Then iPageCurrent = 1

    If iPageCount = 0 Then
    Response.Write "No records found!"
    Else
    objPagingRS.Abs olutePage = iPageCurrent

    Dim arrDBData
    ReDim arrDBData(iFiel dCount, iRecordCount)
    LoopRecordCount = 0

    'arrDBData = objPagingRS.Get Rows()

    Do While LoopRecordCount < iPageSize And Not objPagingRS.EOF
    For I = 0 To objPagingRS.Fie lds.Count - 1
    arrDBData(I, LoopRecordCount ) = objPagingRS.Fie lds(I)
    Next
    LoopRecordCount = LoopRecordCount + 1
    objPagingRS.Mov eNext
    Loop

    objPagingRS.Clo se
    Set objPagingRS = Nothing
    objPagingConn.C lose
    Set objPagingConn = Nothing
    End If

    iRecordCount = iRecordCount - 1
    iFieldCount = iFieldCount - 1
    LoopRecordCount = LoopRecordCount - 1

    pageNum = Round(iRecordCo unt/iPageSize)
    If pageNum < (iRecordCount/iPageSize) Then pageNum = pageNum + 1

    Response.Write "&nbsp;&nbsp;<b >Page: "
    For counter = 1 To pageNum
    if counter=iPageCu rrent then
    Response.Write "&nbsp;<spa n class=activepag e>&nbsp;"
    Response.Write counter
    Response.Write "&nbsp;</span>"
    markShowPage=1
    else
    Response.Write "&nbsp;<a onclick=exitoff () href=db_paging_ array.asp?page= "
    Response.Write counter
    Response.Write " class=listingLi nk>"
    Response.Write counter
    Response.Write "</a>"
    end if
    Next
    Response.Write "</b>"

    Response.Write "&nbsp;&nbs p;"

    Response.Write "<b>"

    Response.Write "<a href=db_paging_ array.asp?page= "
    If iPageCurrent = 1 Then
    Response.Write pageNum
    else
    Response.Write iPageCurrent-1
    end if
    Response.Write " class=listingLi nk>Back</a>&nbsp;&nbsp; "
    Response.Write "<a href=db_paging_ array.asp?page= "
    If iPageCurrent = pageNum Then
    Response.Write "1"
    else
    Response.Write iPageCurrent+1
    end if
    Response.Write " class=listingLi nk>Next</a>"

    Response.Write "</b>"

    %>
    <p>

    <table border="1">
    <%
    For I = 0 To LoopRecordCount
    Response.Write "<tr>" & vbCrLf
    For J = 0 To ifieldCount
    Response.Write vbTab & "<td>" & arrDBData(J, I) & "</td>" & vbCrLf
    Next ' J
    Response.Write "</tr>" & vbCrLf
    Next ' I
    %>
    </table>


    <style>

    body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    }

    td {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    }

    listingLink {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    font-weight: bold;
    color: blue;
    text-decoration:none ;
    margin-left:0px;
    margin-right:0px;
    }

    a.listingLink:h over {
    color:#FF0000;
    }

    activepage {
    color:#ffffee;
    background-color: #000033;
    font-weight: bold;
    }
    </style>



    Originally posted by nirmalsingh
    hi everybody,
    as in ( the scripts) page below, i want to do some pagination in c#.
    for example : <<previous>> 1 2 3 4 5 . . . . . 51 52 53 <<next>>
    help me plz..

    i have build htmltable using c# just like

    Response.Write( "<table">);
    ..........
    ..........
    ..........
    Response.Write( "</table">);

    i want display only 10 records in a table, and also i know total number of records help me plz.

    Comment

    • nirmalsingh
      New Member
      • Sep 2006
      • 218

      #3
      i am loading radio buttons in the first column of every rows, and someone says client side control will be loaded more faster then web controls, i am starter to .net. is there is any way to load controls in datagrid? if s give sample code for reference plz

      Comment

      • radcaesar
        Recognized Expert Contributor
        • Sep 2006
        • 759

        #4
        Yes thats true. But that will not affect the application's performance like you think. I will suggest the datagrid for this purpose.

        After you add the controls like this, refering that particular control will lead you with lot of work arounds. When compared to the same, datagrid is preferable.

        :)

        Originally posted by nirmalsingh
        i am loading radio buttons in the first column of every rows, and someone says client side control will be loaded more faster then web controls, i am starter to .net. is there is any way to load controls in datagrid? if s give sample code for reference plz

        Comment

        Working...