Click here to Skip to main content
15,896,329 members
Articles / Web Development / ASP.NET

Load while scrolling

Rate me:
Please Sign up or sign in to vote.
4.82/5 (6 votes)
27 Jul 2012CPOL4 min read 32.2K   863   28  
Following Article will show how to load real time data while user scrolls.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using ScrollingSample.Data;
using System.Web.Http;

namespace ScrollingSample.Controllers
{
    public class ProductController : ApiController
    {
        NorthWindDataContext _context = new NorthWindDataContext();

        /// <summary>
        /// Gets the specified page size.
        /// </summary>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        public List<Model.Product> Get()
        {
            List<Model.Product> products = new List<Model.Product>();

         
                products = (from e in _context.Products
                             select new Model.Product
                             {
                                 ProductID = e.ProductID,
                                 ProductName = e.ProductName,
                                 SupplierName = e.Supplier.CompanyName,
                                 CategoryName = e.Category.CategoryName,
                                 UnitPrice = Convert.ToDecimal(e.UnitPrice)
                             }).ToList();
            
            return products;
        }

        /// <summary>
        /// Gets the specified page size.
        /// </summary>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        public List<Model.Product> GetProductsByPage(int pageSize, int currentPage, string mode)
        {
            List<Model.Product> products = new List<Model.Product>();

            if (string.Equals("XML", mode, StringComparison.OrdinalIgnoreCase)) //if XML
            {
                string xmlPath = HttpContext.Current.Server.MapPath(@"/Data/Product.xml");
                XElement xml = XElement.Load(xmlPath);

                products = xml.Descendants("Product")
                    .Select(e => new Model.Product
                    {
                        ProductID = Convert.ToInt32(e.Descendants("ProductID").FirstOrDefault().Value),
                        ProductName = e.Descendants("ProductName").FirstOrDefault().Value,
                        SupplierName = e.Descendants("SupplierName").FirstOrDefault().Value,
                        CategoryName = e.Descendants("CategoryName").FirstOrDefault().Value,
                        UnitPrice = Convert.ToDecimal(e.Descendants("UnitPrice").FirstOrDefault().Value)
                    }).Skip(pageSize * currentPage).Take(pageSize).ToList();

            }
            else //if SQL
            {
                products = (from e in _context.Products
                             select new Model.Product
                             {
                                 ProductID = e.ProductID,
                                 ProductName = e.ProductName,
                                 SupplierName = e.Supplier.CompanyName,
                                 CategoryName = e.Category.CategoryName,
                                 UnitPrice = Convert.ToDecimal(e.UnitPrice)
                             }).Skip(pageSize * currentPage).Take(pageSize).ToList();
            }
            return products;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
9+ plus years of experience in IT industry. This includes experience in architecting, designing and developing solutions on Web and desktop application platforms

Comments and Discussions