Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   User Control Code For Paging In ASP.NET 3.5 (http://www.go4expert.com/articles/user-control-code-paging-aspnet-35-t19323/)

naimish 7Sep2009 07:11

User Control Code For Paging In ASP.NET 3.5
 

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 :)


shabbir 3Oct2009 17:07

Re: User Control Code For Paging In ASP.NET 3.5
 
Nomination for Article of the month - Sep 2009 Started. Nominate this article.


All times are GMT +5.5. The time now is 01:19.