aspx Tutorial

NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services,
Advertise Here

Toolbar

Get our toolbar!

Advertize



Posted by Aamir Hasan   on Sunday, June 6, 2010 Total Views:  

I have made this project in visual studio 2010, using Linq you must have Frame work 3.O. In Repeater there is no build in paging. I will tell you how to implement custom paging using Linq.

LINQ stands for Language Integrated Query. LINQ has a set of querying operators that can be used to query in memory object collection, Sql database, XML, etc.To create pages using Linq, there are two methods are:

  1. Skip Method
  2. Take Method

 

Skip Method

To skip  a number of records is called Skip Method (Skip()). For more info visit microsoft msdn IQueryable<T>.Skip Method

 

Take Method

To Take a number of records is called Take Method(Take()). For more info visit microsoft msdn Queryable.Take<TSource> Method

Here i am going to Extend the Countries Class.

First we create a method which will return number of records.

 

public  int SelectCount()
    {
        return Select().Count();
    }

 

 

Now the main function is SelectPage, SelectPage will takes two parameters, StartRowIndex and MaximumRows. StartRowIndex is the starting row and MaximumRows is define that how many rows you want to get and show in a page. In this function we need two methods Skip() and Take(). These two methods will return the records between the rows starting at the Take values and rest of all record will be skip from whole records.

 

 

public List Select()
    {

        return _Countries;
    }

    public  List SelectPage(int startRowIndex, int maximumRows)
    {
        return Select().Skip(startRowIndex).Take(maximumRows).ToList();
    }

 

 

 

The entire class extension will look something like this:

 

using System.Collections.Generic;
using System.Linq;
/// 
/// Author:Aamir Hasan
/// Phone no:092 333 5494532
/// 
public class Countries : CountriesBaseClass
{
    public Countries()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    List _Countries = new List
    {
       
        new CountriesBaseClass { CountryID=1,CountryName= "Afghanistan", CountryCode= "9AF"},
        new CountriesBaseClass { CountryID=2,CountryName= "Albania",CountryCode=  "9AL"}, 
        new CountriesBaseClass { CountryID=3,CountryName= "Algeria", CountryCode= "9AG"}, 
        new CountriesBaseClass { CountryID=4,CountryName=  "Andorra",CountryCode= "9AN"},
        new CountriesBaseClass { CountryID= 5,CountryName= "Angola", CountryCode= "9AO"}, 
        new CountriesBaseClass { CountryID= 6,CountryName= "Anguilla",CountryCode=  "9AV"}, 
        new CountriesBaseClass { CountryID=7,CountryName= "Antigua and Barbuda", CountryCode= "9AC"}, 
        new CountriesBaseClass { CountryID=8,CountryName= "Argentina", CountryCode= "9AR"}, 
        new CountriesBaseClass { CountryID=9,CountryName= "Ashmore Cartier Islands", CountryCode= "9AT"}, 
        new CountriesBaseClass { CountryID=10,CountryName=  "Australia",CountryCode=  "9AS"}, 
        new CountriesBaseClass { CountryID= 11,CountryName= "Austria", CountryCode= "9AU"}, 
        new CountriesBaseClass { CountryID= 12,CountryName=  "Bahamas, The", CountryCode= "9BF"}, 
        new CountriesBaseClass { CountryID= 13,CountryName=  "Bahrain", CountryCode= "9BA"}, 
        new CountriesBaseClass { CountryID=14,CountryName=  "Bangladesh",CountryCode=  "9BG"}, 
        new CountriesBaseClass { CountryID=15,CountryName=  "Barbados", CountryCode= "9BB"}, 
        new CountriesBaseClass { CountryID=16,CountryName=  "Bassa da India", CountryCode= "9BS"}, 
        new CountriesBaseClass { CountryID= 17,CountryName=  "Belgium", CountryCode= "9BE"}, 
        new CountriesBaseClass { CountryID=18,CountryName=  "Belize", CountryCode= "9BH"}, 
        new CountriesBaseClass { CountryID= 19,CountryName=  "Benin", CountryCode= "9DM"}, 
        new CountriesBaseClass { CountryID= 20,CountryName=  "Bermuda",CountryCode=  "9BD"}, 
        new CountriesBaseClass { CountryID= 21,CountryName= "Bhutan", CountryCode= "9BT"}, 
        new CountriesBaseClass { CountryID= 22,CountryName=  "Bolivia",CountryCode=  "9BL"}, 
        new CountriesBaseClass { CountryID=23,CountryName=  "Botswana", CountryCode= "9BC"}, 
        new CountriesBaseClass { CountryID=24,CountryName=  "Bouvet Island",CountryCode=  "9BV"}, 
        new CountriesBaseClass { CountryID= 25,CountryName=  "Brazil", CountryCode= "9BR"}, 
        new CountriesBaseClass { CountryID= 26,CountryName=  "British Indian Ocean Territory", CountryCode= "910"}, 
        new CountriesBaseClass { CountryID= 27,CountryName=  "British Virgin Islands",CountryCode=  "9VI"}, 
        new CountriesBaseClass { CountryID= 28,CountryName=  "Brunei", CountryCode= "9BX"}, 
        new CountriesBaseClass { CountryID= 29,CountryName=  "Bulgaria",CountryCode=  "9BU"}, 
        new CountriesBaseClass { CountryID= 30,CountryName=  "Burma", CountryCode= "9BM"}, 
        new CountriesBaseClass { CountryID= 31,CountryName=  "Burundi", CountryCode= "9BY"}, 
        new CountriesBaseClass { CountryID= 32,CountryName=  "Cameroon",CountryCode=  "9CM"}, 
        new CountriesBaseClass { CountryID= 33,CountryName=  "Canada",CountryCode=  "9CA"}, 
        new CountriesBaseClass { CountryID= 34,CountryName=  "Cape Verde",CountryCode=  "9CV"}, 
        new CountriesBaseClass { CountryID= 35,CountryName=  "Cayman Islands",CountryCode=  "90"}, 
        new CountriesBaseClass { CountryID= 36,CountryName=  "Central African Republic", CountryCode= "9CT"}, 
        new CountriesBaseClass { CountryID=37,CountryName=  "Chad",CountryCode=  "9CD"}, 
        new CountriesBaseClass { CountryID=38,CountryName=  "Chile",CountryCode=  "9CI"}, 
        new CountriesBaseClass { CountryID= 39,CountryName=  "China",CountryCode=  "9CH"}, 
        new CountriesBaseClass { CountryID= 40,CountryName=  "Christmas Island",CountryCode=  "9KT"}
    };



    public List Select()
    {

        return _Countries;
    }

    public  List SelectPage(int startRowIndex, int maximumRows)
    {
        return Select().Skip(startRowIndex).Take(maximumRows).ToList();
    }

    public  int SelectCount()
    {
        return Select().Count();
    }

}

 

 

we are now doen the Countries class and can implement the functions in our aspx.cs Page. Now we have to create a Object of countires which will return the country list on page load.

 

private int _NoOfRecords = 6;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["setCurrentPage"] = 0;
            Countries Coun = new Countries();
            IsFirstPage();
            Repeater1.DataSource = Coun.SelectPage(0, _NoOfRecords);
            Repeater1.DataBind();
        }

    }

 

 

Create a getCurrentPage Method which will set or get in a viewState a current page number.

 

public int getCurrentPage
    {
        get
        {
            System.Object obj = this.ViewState["setCurrentPage"];

            if (obj == null)
                return 0;
            else
                return Convert.ToInt32(obj);
        }

        set
        {
            ViewState["setCurrentPage"] = value;

        }

    }

 

Next Create two Previous and Next button events which will decrement the value of getCurrentPage by one (1) and on the Next button Click, Event will Increment the value of getCurrentPage by one (1).

 

protected void lnkNext_Click(object sender, EventArgs e)
    {

        getCurrentPage++;
        BindRepeaterWithPaging();
    }
    protected void lnkPrevious_Click(object sender, EventArgs e)
    {
        getCurrentPage--;
        BindRepeaterWithPaging();
    }

 

Create IsFirstPage a function which will Restrict to Next and Previous button between the no.of records.

 

 

protected void IsFirstPage()
    {
        Countries Coun = new Countries();
        var Count = Coun.SelectCount();

        if (getCurrentPage == 0 && Count >= 1)
        {
            lnkPrevious.Visible = false;
            lnkNext.Visible = true;

        }
        if (getCurrentPage > 0 && Count >= 1)
        {
            lnkPrevious.Visible = true;
            lnkNext.Visible = true;
        }
        if (getCurrentPage != 0 && _NoOfRecords >= (Count / getCurrentPage))
        {
            lnkPrevious.Visible = true;
            lnkNext.Visible = false;
        }
        litPageNumber.Text = string.Format("Page: {0} of {1}", (getCurrentPage + 1).ToString(),Count);

    }

 

Make a function which will Bind a Repeater.

 

protected void BindRepeaterWithPaging()
    {
        Countries Coun = new Countries();  
        IsFirstPage();
        Repeater1.DataSource = Coun.SelectPage(getCurrentPage * _NoOfRecords, _NoOfRecords);
        Repeater1.DataBind();

    }

 

In ASPX page , we need Repeater control. Drag and drop repeater control from toolbox,Data. if you can not see the toolbox on visual studio then goto menu at top click on View -> toolbox Shortcut  to open toobox click from keyboard (CTLR + ALT + X).

 

Download

Custom Paging Repeater III.rar (124.36 kb)

 

Demo

Click here to view online Demo

Protected by Copyscape Online Plagiarism Tool
Comments are closed
Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5