Paged Data Source - Custom Paging

PagedDataSource, is a class that encapsulates the paging related properties for data-bound controls such as DataGrid, GridView, DataList, DetailsView and FormView that allow it to perform paging. And this article is going to combine both DataList control and PagedDataSource class to explain dynamic or custom paging methods for Asp.Net developers.
protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            LoadPhoto();
        }
    }
    catch (Exception Exc)
    {
        HandleException = Exc;
    }
}

// LoadPhoto Function
private void LoadPhoto()
{
    PagedDataSource objPhotoPage = new PagedDataSource();
    objPhotoPage.AllowPaging = true;

    SOBLImageGallery _SOBLImageGallery = new SOBLImageGallery();
    DataSet dsImageGallery = _SOBLImageGallery.Select6Records(new SOENTImageGallery());


    if (dsImageGallery != null && dsImageGallery.Tables.Count > 0 && dsImageGallery.Tables[0].Rows.Count > 0)
    {
        objPhotoPage.DataSource = dsImageGallery.Tables[0].DefaultView;
        objPhotoPage.PageSize = 8;
        objPhotoPage.CurrentPageIndex = int.Parse(lblCurrentPhotoPage.Text) - 1;
        dlPhoto.DataSource = objPhotoPage;
        dlPhoto.DataBind();

        btnNextPhoto.Visible = !objPhotoPage.IsLastPage;
        btnPrevPhoto.Visible = !objPhotoPage.IsFirstPage;

    }
    else
    {
        dlPhoto.DataSource = null;
        dlPhoto.DataBind();
        btnNextPhoto.Visible = false;
        btnPrevPhoto.Visible = false;

    }
}                                                                                                                  

// Next button Click (btnNextPhoto_Click)

protected void btnNextPhoto_Click(object sender, EventArgs e)
{
    lblCurrentPhotoPage.Text = Convert.ToString(int.Parse(lblCurrentPhotoPage.Text) + 1);
    LoadPhoto();
}

// Previous button Click (btnPrevPhoto_Click)

protected void btnPrevPhoto_Click(object sender, EventArgs e)
{
    lblCurrentPhotoPage.Text = Convert.ToString(int.Parse(lblCurrentPhotoPage.Text) - 1);
    LoadPhoto();
}

Comments

Popular posts from this blog

Page Life Cycle in ASP.NET

Managed Code and UnManaged Code

Select nth Row from Table in SQL Server

Abstract Class and An Interface

SQL Server 2005 with XML Parameters

Print Function By JavaScript

Auto Refresh .aspx page - ASP.NET

Overview of MVC