Automatically paging items for two asp.net repeaters.

Maitha 21 Reputation points
2022-03-27T08:27:28.233+00:00

I want the items inside repeater paged automatically as I have two separate repeaters . each item different question , I want 1,2,3... for each one. 187252-repeater.png

187253-code.png

Note : server is not allowed me to post my code.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,280 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 25,866 Reputation points Microsoft Vendor
    2022-03-28T06:48:19.22+00:00

    Hi @Maitha ,
    To implement default paging in a Repeater control, you can use the PagedDataSource class as a wrapper for the ProductsDataTable whose content is paged.

    The PagedDataSource class has a DataSource property assignable to any enumerable object, PageSize and CurrentPageIndex properties that indicate the number of records displayed per page and the current page index.

    To start paging, you also need to set PagedDataSource.AllowPaging = true, you can refer to the documentation for details.
    Paging Report Data in a DataList or Repeater Control (VB)
    PagedDataSource Class

    Note:You can screenshot the code or post it in notepad.

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Albert Kallal 4,731 Reputation points
    2022-03-28T07:16:50.083+00:00

    Well, I would consider in place of a repeater, consider using a FormView. They are very simular, but like GridView, it supports built in paging.

    While the ListView directly supports a pager control dropped on the page?

    GridView, and FormView do support a pager property setting.

    And even better? In most cases the Repeater control can be re-named as FormView.

    (or just drop in a FormView, and copy the "item template over from the repeater).

    And you can css style the Pager.

    So, while a repeater, and FormView look similar, the Formview can display 1 at a time, and hence this:

    187395-image.png

    It not clear if you looking to display more then 1 "reocrd", or you want 1 reocrd at a time? (formview will give you 1 record at a time as per above.

    So, you could have 3 FormViews, that would say show 3 questions, and then each would have individual paging.

    And like GridView, you can choose numeric, or next/previous. Say like this:

    187405-image.png

    And like GridView - it does not really require much code at all. (i fact none if you using a DataSource on the page).

    Now, I have a css style for the above buttons, since the default as we all know looks like say this:

    187432-image.png

    So, consider a FormView - they are high compatible with a Repeater - but with paging, you get one at a time.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada


  3. Maitha 21 Reputation points
    2022-03-29T05:47:14.38+00:00

    how set page size ? I want to display 4 items in one page in formview ?


  4. Albert Kallal 4,731 Reputation points
    2022-03-29T16:46:02.24+00:00

    As noted, I suggested a Fromview, if you wanted to page each of the 3 (or 4) questions displyed.

    I kind of assumed, you want 3 questions, but each could page, so say you display 3 "secitons"

    Physics:
    Question text
    "pager here"

    Math:
    Math question text"
    Pager here

    Biology
    Question text
    Pager here

    So, we could display 3 sections, each with a pager.

    But, you want say 3 (or 4) questions. For that I would use a repeater, and simple "build" and "cook up" your OWN data paging. This way, you can drop in one repeater - and FEED it ONLY 3 questions.

    I don't have sample questions. But, say we drop in a repeater (don't care what you have inside - not important).

    So, say we have this markup:

    188031-image.png

    Now, I hid the repeater markup above - (markup in repeater not important here), but NOTE the 2 buttons below - our next, previous.

    So, the code to control this now becomes this:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
      
            If Not IsPostBack Then  
      
                Session("PageNumber") = 1   ' start at page 1 of questions  
                Session("PageSize") = 3      ' number questions per page  
                LoadData()  
      
            End If  
        End Sub  
      
      
        Sub LoadData()  
      
            Using conn As SqlConnection = New SqlConnection(My.Settings.TEST4)  
      
                Dim strSQL As String =  
                    "SELECT * FROM tblHotelsA ORDER BY HotelName"  
      
                ' we change above to this for paging:  
                strSQL =  
                    "SELECT * FROM tblHotelsA ORDER BY HotelName  
                    OFFSET ((@PageNumber - 1) * @RowspPage) ROWS  
                    FETCH NEXT @RowspPage ROWS ONLY"  
      
                Using cmd As SqlCommand = New SqlCommand(strSQL, conn)  
      
                    cmd.Parameters.Add("@PageNumber", SqlDbType.Int).Value = Session("PageNumber")  
                    cmd.Parameters.Add("@RowspPage", SqlDbType.Int).Value = Session("PageSize")  
                    conn.Open()  
      
                    Dim rstData As New DataTable  
                    rstData.Load(cmd.ExecuteReader)  
                    Repeater2.DataSource = rstData  
                    Repeater2.DataBind()  
                End Using  
            End Using  
      
            ' deal with next/previous buttons and "info"  
            Dim intCurrentPage As Integer = Session("PageNumber")  
            Dim intStart As Integer = ((intCurrentPage - 1) * Session("PageSize")) + 1  
            Dim intEnd As Integer = intStart + Session("PageSize") - 1  
      
            lblNextInfo.Text = "Viewing Questions " & intStart & " to " & intEnd  
      
        End Sub  
    

    Really, not much more code then just loading up the repeater.

    Now, we have to code up our next/previous buttons.

    They are quite simple, say like this:

    Protected Sub cmdNext_Click(sender As Object, e As EventArgs) Handles cmdNext.Click  
    
        Session("PageNumber") += 1  
        LoadData()  
    
    End Sub  
    
    Protected Sub cmdPrevious_Click(sender As Object, e As EventArgs) Handles cmdPrevious.Click  
    
        Session("PageNumber") -= 1  
        LoadData()  
    
    End Sub  
    

    Now, of course you probably on start up get the (query) the total number of questions, and thus could "enable" or hide the next/previous buttons. But, when we run the above, I'll get 3 repeaters like this:

    187914-image.png

    so, really, I would NOT use built in paging, since the issue is this is not a grid or table, and while paging probably COULD work VERY well if you adopted a ListView in this case?

    Since you already have a repeater setup, then building your own custom pager code as per above is most likely the path of least efforts. As noted, you have to add to above - since I would "query" and get the total number of questions, and also say if on page 1, then the previous button of course has to be disabled, or a message given that your can't move in that direction anymore. But, the meat and potatoes, and concept of above should work well, and it not really much more code then having to load up the repeater anyway.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments