Custom Paging in Asp.Net without using controls like GridView and Repeater

Custom Paging in Asp.Net without using controls like GridView and Repeater

You can implement Custom Paging in ASP.NET using just HTML controls and not using ASP.NET controls like GridView and repeater. Whenever their is a need to have ASP.NET Paging in you website, we always advice you to use Custom Paging. In this tutorial we will show how to do it in step by step manner.

See Custom Paging Stored Procedure at Custom Paging with SQL Server 2012

STEP 1:-First of all in our .aspx page we will create server side ‘div’ which will show the dynamic records which are fetched through custom paging stored procedure.

 
<div id="recordDiv" runat="server">
</div>

STEP 2:- Create a label control in the .aspx page which will show the paging links

<asp:Label ID="pagingLabel" runat="server"></asp:Label>

STEP 3:- In the page load event call the function which will fetch the current page records from the custom paging stored procedure.

if (!IsPostBack)
    Fetch();

protected void Fetch()
{
    int pageNo = Request.QueryString["pn"] == null ? 1 : Convert.ToInt32(Request.QueryString["pn"]);
    int pageSize = 10;
    DataTable dataTable = new DataTable();
    int totalRecords;

    //Fill the records into ‘dataTable’ with custom paging stored procedure
    //Put total records from the datasource into variable ‘totalRecords’
                         
    if (dataTable.Rows.Count > 0)
    {
        pagingLabel.Text = Set_Paging(pageNo,pageSize, totalRecords, "", "myaccount.aspx", "");
        //Bind ‘recordDiv’ with the values of filled in dataTable
    }
    else
        orderH1.InnerHtml = "No Data"; //showing no data in a div
}

STEP 4:- Add the Set_Paging() function which will create the paging link. Note that the Set_Paging function is called in the Fetch() function above.

public string Set_Paging(Int32 PageNumber, int PageSize, Int64 TotalRecords, string ClassName, string PageUrl, string DisableClassName)
{
    string ReturnValue = "";
    try
    {
        Int64 TotalPages = Convert.ToInt64(Math.Ceiling((double)TotalRecords / PageSize));
        if (PageNumber > 1)
        {
            if (PageNumber == 2)
                ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim() + "?pn=" + Convert.ToString(PageNumber - 1) + "' class='" + ClassName + "'>Previous</a>&nbsp;&nbsp;&nbsp;";
            else
            {
                ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                if (PageUrl.Contains("?"))
                    ReturnValue = ReturnValue + "&";
                else
                    ReturnValue = ReturnValue + "?";
                ReturnValue = ReturnValue + "pn=" + Convert.ToString(PageNumber - 1) + "' class='" + ClassName + "'>Previous</a>&nbsp;&nbsp;&nbsp;";
            }
        }
        else
            ReturnValue = ReturnValue + "<span class='" + DisableClassName + "'>Previous</span>&nbsp;&nbsp;&nbsp;";
        if ((PageNumber - 3) > 1)
            ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim() + "' class='" + ClassName + "'>1</a>&nbsp;.....&nbsp;|&nbsp;";
        for (int i = PageNumber - 3; i <= PageNumber; i++)
            if (i >= 1)
            {
                if (PageNumber != i)
                {
                    ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                    if (PageUrl.Contains("?"))
                        ReturnValue = ReturnValue + "&";
                    else
                        ReturnValue = ReturnValue + "?";
                    ReturnValue = ReturnValue + "pn=" + i.ToString() + "' class='" + ClassName + "'>" + i.ToString() + "</a>&nbsp;|&nbsp;";
                }
                else
                {
                    ReturnValue = ReturnValue + "<span style='font-weight:bold;'>" + i + "</span>&nbsp;|&nbsp;";
                }
            }
        for (int i = PageNumber + 1; i <= PageNumber + 3; i++)
            if (i <= TotalPages)
            {
                if (PageNumber != i)
                {
                    ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                    if (PageUrl.Contains("?"))
                        ReturnValue = ReturnValue + "&";
                    else
                        ReturnValue = ReturnValue + "?";
                    ReturnValue = ReturnValue + "pn=" + i.ToString() + "' class='" + ClassName + "'>" + i.ToString() + "</a>&nbsp;|&nbsp;";
                }
                else
                {
                    ReturnValue = ReturnValue + "<span style='font-weight:bold;'>" + i + "</span>&nbsp;|&nbsp;";
                }
            }
        if ((PageNumber + 3) < TotalPages)
        {
            ReturnValue = ReturnValue + ".....&nbsp;<a href='" + PageUrl.Trim();
            if (PageUrl.Contains("?"))
                ReturnValue = ReturnValue + "&";
            else
                ReturnValue = ReturnValue + "?";
            ReturnValue = ReturnValue + "pn=" + TotalPages.ToString() + "' class='" + ClassName + "'>" + TotalPages.ToString() + "</a>";
        }
        if (PageNumber < TotalPages)
        {
            ReturnValue = ReturnValue + "&nbsp;&nbsp;&nbsp;<a href='" + PageUrl.Trim();
            if (PageUrl.Contains("?"))
                ReturnValue = ReturnValue + "&";
            else
                ReturnValue = ReturnValue + "?";
            ReturnValue = ReturnValue + "pn=" + Convert.ToString(PageNumber + 1) + "' class='" + ClassName + "'>Next</a>";
        }
        else
            ReturnValue = ReturnValue + "&nbsp;&nbsp;&nbsp;<span class='" + DisableClassName + "'>Next</span>";
    }
    catch (Exception ex)
    {
    }
    return (ReturnValue);
}

In this way we can create simple to complex solutions where Custom Paging is involved. Custom Paging will also make your website light weight and fast.

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "WordPress, SEO, jQuery, HTML" and more.

Enter your email address

Subscribe to this blog and receive notifications of new posts by email. Join over 81,000 other subscribers

Don't worry we won't spam.

subscribers