Home > ASP.Net, Linq > Repeater Paging using Linq

Repeater Paging using Linq


Hi

try this example for paging Repeater control using Linq

Step1 :- Open VS2008 and create a new website

Step2 :- add new Linq to SQL DataContext  and name it “Northwind.dbml” and add Customers table in it

Step3 :- Add new Web page and from ToolBox drag Repeater control and PlaceHolder control inside page

<asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label runat="server" ID="lblContactName" Text='<%# Eval("Name") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <table>
        <tr>
            <td>
                <asp:PlaceHolder ID="plcPaging" runat="server" />
            </td>
        </tr>
    </table>

Step4 :- In code behind add the following code


using System;
using System.Linq;
using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page
    {
        private int RowCount
        {
            get
            {
                return (int)ViewState["RowCount"];
            }
            set
            {
                ViewState["RowCount"] = value;
            }
        }
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FetchData(10, 0);    
            }
            else
            {
                plcPaging.Controls.Clear();
                CreatePagingControl();
            }                      
        }

        private void FetchData(int take, int pageSize)
        {          
            using (NorthwindDataContext dc = new NorthwindDataContext())
            {
                var query = from p in dc.Customers
                                .OrderBy(o => o.ContactName)
                                .Take(take)
                                .Skip(pageSize)
                            select new
                            {
                                ID = p.CustomerID,
                                Name = p.ContactName,
                                Count = dc.Customers.Count()
                            };
               
                PagedDataSource page = new PagedDataSource();
                page.AllowCustomPaging = true;
                page.AllowPaging = true;
                page.DataSource = query;
                page.PageSize = 10;
                Repeater1.DataSource = page;
                Repeater1.DataBind();

                if (!IsPostBack)
                {
                    RowCount = query.First().Count;
                    CreatePagingControl();
                }
            }
        }

        private void CreatePagingControl()
        {  
            for (int i = 0; i < (RowCount / 10) + 1; i++)
            {
                LinkButton lnk = new LinkButton();               
                lnk.Click += new EventHandler(lbl_Click);
                lnk.ID = "lnkPage" + (i + 1).ToString();
                lnk.Text = (i + 1).ToString();
                plcPaging.Controls.Add(lnk);
                Label spacer = new Label();
                spacer.Text = "&nbsp;";
                plcPaging.Controls.Add(spacer);
            }
        }
      
        void lbl_Click(object sender, EventArgs e)
        {
            LinkButton lnk = sender as LinkButton;
            int currentPage = int.Parse(lnk.Text);
            int take = currentPage * 10;
            int skip = currentPage == 1 ? 0 : take - 10; 
            FetchData(take, skip);
        }

}

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net, Linq Tags:
  1. December 15, 2011 at 6:40 pm

    Works perfectlly!
    Thanks for help!

  2. yasserzaid
    December 19, 2011 at 7:15 pm

    @Marcus :- you are welcome 🙂

  3. Nitish Raina
    January 25, 2012 at 6:00 am

    This example is good but can you enlighten me if there are 1000’s of rows then the number of paging link button will also increase this will make the pager link buttons to fill the page abnormally, See this pic. http://i1143.photobucket.com/albums/n636/ninjato_/rpt_pg.png .

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: