.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Paging in DataList Using LINQ

Posted By:Sunil Yadav       Posted Date: May 28, 2010    Points: 10    Category: LINQ    URL: http://www.dotnetspark.com  

In this code snippet i am going to show how we can implement paging in DataList using LINQ.
 

This solution uses four buttons for navigation i.e. first,prev,next and last.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DataListPaging._Default" %>




    Paging In DataList using Linq


    
    
SubProduct_ID:
SubProduct_Name:
Price:
Description:
/Code Behind
using System;
using System.Linq;

namespace DataListPaging
{
    public partial class _Default : System.Web.UI.Page
    {
        int iCurrentPage;
        int iSkipCount;
        int iPageSize = 10;//Hard code value 10 indicated page size to 10
        static int iLastSkipCount;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                iCurrentPage = 1;
                btnNext.Attributes.Add("currentpage", iCurrentPage.ToString());
                iLastSkipCount = GetProducts().Count();
                var vSubProductMaster = GetProducts().Take(iPageSize);
                dtlProduct.DataSource = vSubProductMaster;
                dtlProduct.DataBind();
            }
        }

        private IQueryable GetProducts()
        {
            TrainingDataContext db = new TrainingDataContext();

            var vSubproduct = from p in db.SubProductMasters
                              select p;
            return vSubproduct;
        }

        //moves to first record
        protected void btnFirst_Click(object sender, EventArgs e)
        {
           
            iCurrentPage = 1;
            btnNext.Attributes["currentpage"] = iCurrentPage.ToString();
            var vFirst = GetProducts().Select(p => new { p.SubProduct_ID, p.SubProduct_Name, p.Price, p.Description }).Take(iPageSize);
            dtlProduct.DataSource = vFirst;
            dtlProduct.DataBind();
        }

        //moves to Previous Record
        protected void btnPrev_Click(object sender, EventArgs e)
        {
            iCurrentPage = Convert.ToInt32(btnNext.Attributes["currentpage"]);
             iSkipCount = (iCurrentPage - 1) - iPageSize;
            if (iSkipCount >= 0)
            {
                btnNext.Attributes.Add("currentpage", (iCurrentPage - 1).ToString());
                var vNext = GetProducts().Select(p => new { p.SubProduct_ID, p.SubProduct_Name, p.Price, p.Description }).Skip(iSkipCount).Take(iPageSize);
                dtlProduct.DataSource = vNext;
                dtlProduct.DataBind();
            }
            else
            {
            //Exception Messgae
            }
        }

        //moves to Next Record

        protected void btnNext_Click(object sender, EventArgs e)
        {
            iCurrentPage = Convert.ToInt32(btnNext.Attributes["currentpage"]);
            iSkipCount = iCurrentPage * iPageSize-iPageSize;
           //Set the value of currentpage attribute
            if (iSkipCount < iLastSkipCount)
            {
                btnNext.Attributes.Add("currentpage", (iCurrentPage + 1).ToString());
                var vNext = GetProducts().Select(p => new { p.SubProduct_ID, p.SubProduct_Name, p.Price, p.Description }).Skip(iSkipCount).Take(iPageSize);
                dtlProduct.DataSource = vNext;
                dtlProduct.DataBind();
            }
            else
            {
            //Exception Message
            }
        }
        //moves to Last Record
        protected void btnLast_Click(object sender, EventArgs e)
        {
            iCurrentPage = iLastSkipCount-iPageSize;
            btnNext.Attributes["currentpage"] = iCurrentPage.ToString();
            var vLast = GetProducts().Select(p => new { p.SubProduct_ID, p.SubProduct_Name, p.Price, p.Description }).Skip(iLastSkipCount-iPageSize).Take(iPageSize);
            dtlProduct.DataSource = vLast;
            dtlProduct.DataBind();
        }
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend