ASP.NET - paging in repeater control

Asked By asif hameed on 09-Nov-09 12:27 PM

Hi all,

I have a repeater control and I want to show paging in it as follows.

<1 2 3 ... 25>

When user will click three dots,then he will move next 5 pages(if there are any). < and > will move the user to previous and next records. Please suggest me sample code How to do this using asp.net 3.5or ajax. Or i need to use any better control like gridview etc.

Regards,

Asif Hameed

re

Web Star replied to asif hameed on 09-Nov-09 12:37 PM

if u don't have specific reason for useing repeater control then u will be user gridview,

otherwise u can do paging in repeater control as follows

Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim myConnection As New _
      SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    Dim myDA As New SqlClient.SqlDataAdapter("Select * from dtsDefect", _
                                                             myConnection)
    myDA.Fill(ds, "t1")
    pageds.DataSource = ds.Tables("t1").DefaultView
    pageds.AllowPaging = True
    pageds.PageSize = 4
    Dim curpage As Integer

    If Not IsNothing(Request.QueryString("Page")) Then
        curpage = Convert.ToInt32(Request.QueryString("Page"))
    Else
        curpage = 1
    End If

    pageds.CurrentPageIndex = curpage - 1
    lblCurrpage.Text = "Page: " + curpage.ToString()

    If Not pageds.IsFirstPage Then
        lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + _
                                       "?Page=" + CStr(curpage - 1)
    End If

    If Not pageds.IsLastPage Then
        lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + _
                                       "?Page=" + CStr(curpage + 1)
    End If

    Repeater1.DataSource = pageds
    Repeater1.DataBind()
End Sub

GridView

F Cali replied to asif hameed on 09-Nov-09 12:44 PM

I suggest using a GridView instead of a Repeater.  When using a GridView, you can specify the PagerSettings and set it to use NumericFirstLast, meaning it will give you the numeric pages plus the First and Last pages.  If there are more than 10 pages, it will give you the ... in between.

For more details about the PagerSettings, you can go here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.pagersettings.aspx


Regards,
http://www.sql-server-helper.com/error-messages/msg-8001-8500.aspx

Yes, I would choose a GridView for such a requirement.

[)ia6l0 iii replied to asif hameed on 09-Nov-09 01:06 PM
However, you can use a PagedDataSource and configure its Pager items and use it as a datasource for the Repeater control.

PagedDataSource pds = new PagedDataSource();
pds.AllowPaging = true;
pds.PageSize = 3;

//set the datasource to the PagedDataSource
pds.DataSource = myDataSet.Tables["data"].DefaultView;

//bind the PagedDataSource to the Repeater.
myRepeater.DataSource = pds;
myRepeater.DataBind();

Don't forget to update the PagedDataSource's current pageIndex and rebind the repeater. And yes, you need to create the buttons on your own.
asif hameed replied to [)ia6l0 iii on 09-Nov-09 02:02 PM

Hi all,

Thanks for replies. How can i move 5 steps forward by clicking ... Also [)ia6l0 iii , how and when to updated paged data source?


Regards,

Asif Hameed

OK.
[)ia6l0 iii replied to asif hameed on 09-Nov-09 09:17 PM
a) Create a property in your page that holds the page number that you are currently in. Let's call it "CurrentPageNumber". In order to persist the value across postbacks, let's use viewstate.
public int CurrentPageNumber
{
   get
   {
       object o = this.ViewState["_ CurrentPageNumber"];
       if (o == null) return 0; 
       else return (int) o;
   }
   set
   {
       ViewState["_ CurrentPageNumber"] = value;
   }

b) Add the navigation buttons : Previous, Next 
You can set up links to the same page with page numbers as Querystring and in page_load event capture them. 

Other way is to update the PagedDataSource with the right page number and rebind it to the Repeater. On the Previous button click you would do this:
pds .CurrentPageIndex = CurrentPageNumber- 1;
//bind the PagedDataSource to the Repeater.
myRepeater.DataSource = pds;
myRepeater.DataBind();

And on NextButton click, you would move to next page. Like:
pds .CurrentPageIndex = CurrentPageNumber+ 1;
//bind the PagedDataSource to the Repeater.
myRepeater.DataSource = pds;
myRepeater.DataBind();