Loading ...

Implement: Paging in Repeater or Datalist Control | CodeAsp.Net

Implement: Paging in Repeater or Datalist Control

  • 0
    /5
    Avg: 0/5: (0votes)
  • (2)
  • (12470)
Published: 11/18/2009 by Vijendra Shakya

Download SourceCode
As we all knows that Repeater and DataList does not have auto paging support technique like Gridview or Datagrid,
but we can achieve this through PagedDataSource. By using PagedDataSource we can implement paging in Repeater or DataList. Now in our mind there is question arise what is PagedDataSource.
PagedDataSource is a class which encapsulates the paging related properties which is required to perform paging in data bound control like Repeater/DataList. PagedDataSource class comes under System.Web.UI.WebControls namespace.
We have the following ASPX web page code:

<div>

        <table cellpadding="0" cellspacing="0" border="1">

        <thead>

        <tr>

        <th>

            <b>Name</b>

        </th>

        </tr>

        </thead>

        <tbody>

        <asp:Repeater ID="rptPaging" runat="server">

        <ItemTemplate>

        <tr>

            <td><asp:Label runat="server" ID="lblContactName" Text='<%#Container.DataItem %>' /></td>

        </tr>

        </ItemTemplate>

        </asp:Repeater>

        </tbody>

        </table>

        <table>

        <tr>

        <td>

            <asp:LinkButton ID="lnkPrevious" runat="server"

            OnClick="lnkPrevious_Click" Text="&lt;&lt; Previous Page">

            </asp:LinkButton>

            &nbsp;&nbsp;&nbsp;

            <asp:LinkButton ID="lnkNext" runat="server"

            OnClick="lnkNext_Click" Text="Next Page&gt;&gt;">

            </asp:LinkButton>

        </td>

        <td>

            <asp:Literal ID="litPageNumber" runat="server"></asp:Literal>

        </td>

        </tr>

        </table>

    </div>

 

 Now following is the code for class page:
First we create the property on the page,for get and set the value(index) of currentpage.
We can create the property as like following:
 


  public int CurrentPage

    {

        get

        {

            System.Object obj = this.ViewState["_CurrentPage"];

            if (obj == null)

                return 0;

            else

                return Convert.ToInt32(obj);

        }

        set

        {

            ViewState["_CurrentPage"] = value;

        }

    }


Now we can populate the repeater with data, here I have populated the repeater with array list.

 

/// <summary>

    /// Fetches the data.

    /// </summary>

    private void FetchData()

    {

        ArrayList arrayList = new ArrayList();

        arrayList.Add("Vijendra Singh");

        arrayList.Add("Mohd Asif");

        arrayList.Add("Vinay Gupta");

        arrayList.Add("Sumit Arora");

        arrayList.Add("Shaitender Singh");

        arrayList.Add("Mohit Dhaiya");

        arrayList.Add("Ajit Gupta");

        arrayList.Add("Nikhil Verma");

        arrayList.Add("Deepak Nigam");

        arrayList.Add("Paurush Kulshretha");

        arrayList.Add("Manish Gupta");

        arrayList.Add("Umesh Chandra");

        arrayList.Add("Atul Verma");

        arrayList.Add("Idrees Ahmad");

        arrayList.Add("Vasim Parvez");

        arrayList.Add("Vimlesh Kashyap");

        arrayList.Add("Sundeep Sharma");

      

        PagedDataSource objPage;

        objPage = ImplementPaging(arrayList);

        rptPaging.DataSource = objPage;

        rptPaging.DataBind();

    }

 

    /// <summary>

    /// Implements the paging.

    /// </summary>

    /// <param name="list">The list.</param>

    /// <returns>it returns the PagedDataSource</returns>

    protected PagedDataSource ImplementPaging(ArrayList list)

    {
        //'declares the paged data source

        PagedDataSource pageDataSource = new PagedDataSource();
        pageDataSource.DataSource = list;

        pageDataSource.AllowPaging = true;//default value of AllowPaging is false;

        pageDataSource.PageSize = 5;//default value of pageSize is 10;

        pageDataSource.CurrentPageIndex = CurrentPage;

        litPageNumber.Text =string.Format("Page: {0} of {1}",(CurrentPage + 1).ToString(),
                                                         pageDataSource.PageCount.ToString());

        if (pageDataSource.PageCount > 1)

        {

            lnkPrevious.Visible = !pageDataSource.IsFirstPage;

            lnkNext.Visible = !pageDataSource.IsLastPage;

            //lnkPrevious.Enabled = !pageDataSource.IsFirstPage;

            //lnkNext.Enabled = !pageDataSource.IsLastPage;

        }

        else

        {

            lnkPrevious.Visible = false;

            lnkNext.Visible = false;

            //lnkPrevious.Enabled = false;

            //lnkNext.Enabled = false;

        }

        return pageDataSource;

    }

 

On the Page load we write following code:

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            ViewState["_CurrentPage"] = 0;

            FetchData();

        }

    }

Following is the code snippet for next and previous button.
On the Previous button event we decrement the value of CurrentPage by 1(one),
and on the next button’s click event we Increment the value of CurrentPage by 1(one).

 

/// <summary>

    /// Handles the Click event of the lnkPrevious control.

    /// </summary>

    /// <param name="sender">The source of the event.</param>

    /// <param name="e">The <see cref="System.EventArgs"/> instance    containing the event data.</param>

    protected void lnkPrevious_Click(object sender, System.EventArgs e)

    {

        CurrentPage--;

        FetchData();

    }

 

    /// <summary>

    /// Handles the Click event of the lnkNext control.

    /// </summary>

    /// <param name="sender">The source of the event.</param>

    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>

    protected void lnkNext_Click(object sender, System.EventArgs e)

    {

        CurrentPage++;

        FetchData();

    } 


Hope it will help to all.Smile



 

Comments (2)

  
vinay_jss
Vinay Gupta said:
cool!!
3/27/2010
 · 
 
by
  
vijjendra
Nice Job
5/7/2010
 · 
 
by

Top articles

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?