User Control Code For Paging In ASP.NET 3.5

Discussion in 'ASP.NET' started by naimish, Sep 7, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    Introduction



    To privide smooth paging mechanism while delaing with huge quantitys of DATA.

    Background



    Below code snippet can be placed in a user control and can be reused as and when we require paging to be performed on a web UI.

    The code



    Code: C#.NET

    Code:
    private int GetItems()
    {
    	//create new instance of PagedDataSource
    	PagedDataSource objPds = new PagedDataSource();
    	//set number of pages will appear
    	objPds.PageSize = PageSize;
    	objPds.DataSource = Ods.Select();
    	objPds.AllowPaging = true;
    	int count = objPds.PageCount;
    
    
    	objPds.CurrentPageIndex = CurrentPage;
    	if (objPds.Count > 0)
    	{
    		//dispaly controls if there are pages
    		btnPrevious.Visible = true;
    		btnNext.Visible = true;
    		btnLastRecord.Visible = true;
    		btnFirstRecord.Visible = true;
    		lblCurrentPage.Visible = true;
    		lblCurrentPage.Text = "Page " + Convert.ToString(CurrentPage + 1) + " of " + Convert.ToString(objPds.PageCount);
    	}
    	else
    	{
    		//disable controls if there are no pages
    		btnPrevious.Visible = false;
    		btnNext.Visible = false;
    		btnLastRecord.Visible = false;
    		btnFirstRecord.Visible = false;
    		lblCurrentPage.Visible = false;
    	}
    	btnPrevious.Enabled = !objPds.IsFirstPage;
    	btnNext.Enabled = !objPds.IsLastPage;
    	btnLastRecord.Enabled = !objPds.IsLastPage;
    	btnFirstRecord.Enabled = !objPds.IsFirstPage;
    	//check for object control if it a DataList
    	//we will use DList Variable
    	if (ObjectControl is DataList)
    	{
    		DList = (DataList)ObjectControl;
    		DList.DataSource = objPds;
    		DList.DataBind();
    	}
    	//check for object control if it a Repeater
    	//we will use Rep Variable
    	else if (ObjectControl is Repeater)
    	{
    		Rep = (Repeater)ObjectControl;
    		Rep.DataSource = objPds;
    		Rep.DataBind();
    	}
    
    	return count;
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
    	//back to previous page
    	CurrentPage -= 1;
    	GetItems();
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
    	//go to next page
    	CurrentPage += 1;
    	GetItems();
    }
    

    Thanks :)

     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice