-1

I was searching for pagination example in asp.net, by which i can make the solution for my project online exam, in which user can navigate to the next and previous question and also he/she can directly move to the particular question. I googled this query, and i got the solution paging using gridview. Suggestion and example links will be appreciated, Thanks !!

UPDATED

 <asp:GridView ID="gvUserlist" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="Login_Id" DataSourceID="DSUserList" CellPadding="4" ForeColor="#333333"
            GridLines="None" Width="681px">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>

                <asp:BoundField DataField="Login_Id" HeaderText="User Id" ReadOnly="True" SortExpression="Login_Id" />
                <asp:BoundField DataField="User_Type" HeaderText="User Type" SortExpression="User_Type" />
                <asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name" />
                <asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" />
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:CheckBox ID="cbuserid" runat="server" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbuserid" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="DSUserList" runat="server" ConnectionString="<%$ ConnectionStrings:JPConnString %>"
            SelectCommand="SELECT User_Login.Login_Id, User_Login.User_Type, User_Login.Login_Status, derivedtbl_1.First_Name, derivedtbl_1.Last_Name FROM User_Login INNER JOIN (SELECT Email_Id, First_Name, Last_Name FROM User_Info UNION SELECT Email_Id, First_Name, Last_Name FROM Emp_Info) AS derivedtbl_1 ON User_Login.Login_Id = derivedtbl_1.Email_Id WHERE (User_Login.Login_Status = @Login_Status)">
            <SelectParameters>
                <asp:Parameter DefaultValue="Activate" Name="Login_Status" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

As i earlier mentioned that, i need the pagination for online exam, in which user can navigate to next and preview and data will display in column-wise instead of row-wise. As i know, grid-view show the data in row-wise.

Since, in online exam system, i have to display that data not in tabular form, i just need to move to the next row of the table. and i don't think so, grid-view will help me to solve this problem, because it show the data in tabular form

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • I've got an HTML Helper that I use with MVC/Razor. Will that help? – Chase Florell Jul 26 '12 at 16:00
  • PS: You're getting close votes because you haven't shown what you've tried. People (for some lame reason) are more likely to try and close a question rather than vote on it and specify a reason why... encouraging you to update your question. I think this is a valid question if you just update it a little. Show us your work, and explain what you're using (Mvc/Web Forms, etc). – Chase Florell Jul 26 '12 at 16:00
  • 2
    Hello, @coders! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question- if not, research and attempt your question first, and then come back. – Josh Darnell Jul 26 '12 at 16:02
  • For what it's worth, @ChaseFlorell, I tend to [vote to close immediately](http://meta.stackexchange.com/questions/98022/how-soon-should-i-vote-to-close/98026#98026) and then comment (which is why there was some delay between the close-vote and my comment). – Josh Darnell Jul 26 '12 at 16:05
  • 1
    I have read that thread, but I'm definitely in disagreement. Downvoting makes the user take notice just as much, but doesn't discourage them from joining the community. If we continuously close questions on the newer users who don't fully know the "rules" simply turns the community into an "old boys club" rather than nurturing and growing it. – Chase Florell Jul 26 '12 at 16:12
  • @ChaseFlorell I disagree with closure being more discouraging than downvoting. If someone doesn't take the time to learn what is meant by their question being closed, that's their fault. The closed question notices end with "*For help clarifying this question so that it can be reopened, see the [FAQ#close].*" – Josh Darnell Jul 26 '12 at 16:30

2 Answers2

0

The DetailsView sounds like a perfect choice for you. It is designed to show a single element yet still you can bind it to a list of elements and navigate forwards and backwards.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
0

Awe shucks. I don't typically write out the whole code for someone, but I'm feeling particularily generous today.

Here's the VB pager that I use in my project. It's a helper method that I'm using in an MVC application.

    ''' <summary>
    ''' Pagers the specified helper.
    ''' </summary>
    ''' <param name="helper">The helper.</param>
    ''' <param name="urlPrefix">The URL prefix.</param>
    ''' <param name="totalRecords">The total records.</param>
    ''' <param name="currentPage">The current page.</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function Pager(helper As HtmlHelper,
                          urlPrefix As String,
                          totalRecords As Integer,
                          currentPage As Integer) As MvcHtmlString

        ' Get out if we have 5 or less records
        If totalRecords <= 5 Then Return Nothing

        ' Make sure we're not getting invalid pages
        If currentPage <= 0 Then currentPage = 1

        ' Setup our initial variables
        Dim sb1 As New StringBuilder(),
            totalPages = (Math.Round((totalRecords / 5) + 0.5)),
            startingPoint,
            linksAfterCurrent,
            endPoint,
            i


        ' Set boundries for inner link numbers
        Select Case currentPage
            Case totalPages : startingPoint = currentPage - 5
            Case (totalPages - 1) : startingPoint = currentPage - 4
            Case Else : startingPoint = currentPage - 3
        End Select

        Select Case currentPage
            Case 1 : linksAfterCurrent = currentPage + 4
            Case 2 : linksAfterCurrent = currentPage + 3
            Case Else : linksAfterCurrent = currentPage + 2
        End Select

        sb1.Append("<div id=""pagercontainer""><ul class=""pager"">")

        ' Display the previous button and first button
        If currentPage > 1 AndAlso startingPoint >= 1 Then
            If startingPoint > 1 Then sb1.AppendLine([String].Format("<li><a href=""{0}{1}"" title=""go to page {1}"">&laquo;</a></li>", urlPrefix, currentPage - 1))
            sb1.AppendLine([String].Format("<li><a href=""{0}1"" title=""go to page 1"">1</a></li>", urlPrefix))
            If startingPoint > 1 Then sb1.AppendLine("&nbsp;&nbsp;&nbsp;")
        End If


        ' Generate the inner numbers
        i = startingPoint
        While (i < linksAfterCurrent)

            ' This 'if' statement keeps us from building a pager list that's longer than the totalPages.
            If (i >= 0) AndAlso
                (i < totalPages) Then

                ' Build out the inner pager buttons
                sb1.AppendLine([String].Format("<li><a href=""{0}{1}"" {2} title=""go to page {1}"">{1}</a></li>",
                                                urlPrefix,
                                                i + 1,
                                                If(i + 1 = currentPage, "class=""youarehere""", String.Empty)))
            End If

            i += 1
        End While

        endPoint = i

        ' Display the next button and the last button
        If (currentPage < endPoint) AndAlso
            (endPoint < totalPages) Then
            If endPoint < totalPages - 1 Then sb1.AppendLine("&nbsp;&nbsp;&nbsp;")
            sb1.AppendLine([String].Format("<li><a href=""{0}{1}"" title=""go to page {1}"">{1}</a></li>", urlPrefix, totalPages.ToString()))
            If endPoint < totalPages - 1 Then sb1.AppendLine([String].Format("<li><a href=""{0}{1}"" title=""go to page {1}"">&raquo;</a></li>", urlPrefix, currentPage + 1))
        End If


        sb1.Append("</ul></div><div class=""clear""></div>")


        Return MvcHtmlString.Create(sb1.ToString())
    End Function

    ''' <summary>
    ''' Pagers the specified helper.
    ''' </summary>
    ''' <param name="helper">The helper.</param>
    ''' <param name="urlPrefix">The URL prefix.</param>
    ''' <param name="totalRecords">The total records.</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function Pager(helper As HtmlHelper,
                          urlPrefix As String,
                          totalRecords As Integer) As MvcHtmlString
        Return helper.Pager(urlPrefix, totalRecords, 1)
    End Function

And here's how I use it

    @Html.Pager("?page=", Model.TotalEvents, Model.PageNumber)
Chase Florell
  • 46,378
  • 57
  • 186
  • 376