Paging Like Facebook In ASP.NET

Let's learn how to create paging in which we can auto load the content at the bottom after scrolling. Every every time you reach the end of the page, new data will arrive from the database. Here, I will add static content for binding the data in GridView. You can manage or can try this with the database at your end. Just create a table and a stored procedure from which you will pass the page index which will help you get the new data each time you scroll.
 
Description

First, we will take a Webform and add a GridView in this. Then, we will bind the GridView with static content, using For Loop. I have already mentioned that you make it more real by using database stored procedures.

ASPX page(Html)
  1. <form id="form1" runat="server">  
  2.     <asp:ScriptManager ID="scrtmng" runat="server">  
  3.         <Services>  
  4.             <asp:ServiceReference Path="~/WebService.asmx" /> </Services>  
  5.     </asp:ScriptManager>  
  6.     <div style="margin:auto; width:50%;">  
  7.         <asp:GridView ID="grdlst" runat="server" AutoGenerateColumns="true" Width="100%"></asp:GridView>  
  8.         <div id="loading"> <img id="loading-image" src="loading.gif" alt="Loading..." /> </div>  
  9.     </div>  
  10. </form>  
CSS
  1. <style>  
  2.     #loading  
  3.     {  
  4.         width100%;  
  5.         height100%;  
  6.         top: -90px;  
  7.         left: -48px;  
  8.         positionrelative;  
  9.         displayblock;  
  10.         opacity: 1.00;  
  11.         background-color#fff;  
  12.         z-index99;  
  13.         text-aligncenter;  
  14.     }  
  15.       
  16.     #loading-image  
  17.     {  
  18.         positionabsolute;  
  19.         top: 100px;  
  20.         left: 240px;  
  21.         z-index100;  
  22.     }  
  23. </style>  
JavaScript
  1. <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>  
  2. <script>  
  3.     $(document).scroll(function()  
  4.     {  
  5.         if ($(window).scrollTop() + $(window).height() == $(document).height())  
  6.         {  
  7.             //if condition is true then you are reached at the bottom of web page  
  8.             $("#loading").show().delay(4000).queue(function()  
  9.             {  
  10.                 CallWebMethodForMoreRecords();  
  11.                 $(this).dequeue();  
  12.             });  
  13.         }  
  14.     });  
  15.   
  16.     function CallWebMethodForMoreRecords()  
  17.     {  
  18.         WebService.BindGridView(onSuccess);  
  19.     }  
  20.   
  21.     function onSuccess(result)  
  22.     {  
  23.         if (result != "")  
  24.         {  
  25.             $("#grdlst > tbody").append(result);  
  26.             $("#loading").hide();  
  27.         }  
  28.     }  
  29. </script>  
Now, we bind the GridView with the data, as shown below,
  1. public partial class _Default: System.Web.UI.Page   
  2. {  
  3.     protected void Page_Load(object sender, EventArgs e)  
  4.     {  
  5.         List < EmployeeData > lstEmp = new List < EmployeeData > ();  
  6.         for (Int32 i = 1; i <= 50; i++)  
  7.         {  
  8.             EmployeeData objEmp = new EmployeeData();  
  9.             objEmp.EmployeeId = i;  
  10.             objEmp.EmployeeName = "Employee_" + i;  
  11.             objEmp.Designation = "Dot Net Developer";  
  12.             objEmp.Experience = "3 years";  
  13.             lstEmp.Add(objEmp);  
  14.         }  
  15.         grdlst.DataSource = lstEmp;  
  16.         grdlst.DataBind();  
  17.     }  
  18. }  
  19. public class EmployeeData  
  20. {  
  21.     public Int32 EmployeeId {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     public String EmployeeName {  
  26.         get;  
  27.         set;  
  28.     }  
  29.     public String Designation {  
  30.         get;  
  31.         set;  
  32.     }  
  33.     public String Experience {  
  34.         get;  
  35.         set;  
  36.     }  
  37. }  
Here, I just created a simple class, added four properties in it, and added value with the For Loop up to 50. This is just static data but you can bind GridView with the database also. I am doing this to make the concept of paging more clear, here. This is why I am not using the database for fast posting of the article. So, our GridView is bound. If you run the web page, you will see the output. Our work is half one.
 
Now, let's start the second part, i.e., Scrolling the page.

First, what we need to know is that we want to load more data when we have reached the end of the page. So, we need an indication that we have reached the bottom of the page. This we can achieve using jQuery. You can see in the above code that using the scroll event function in jQuery, we get to know if we have reached the end of the page or not.
 
And, what next?
 
Now, we need to load more data when the scroll bar is at the bottom. But, we don't want a post back. So, how we can do this? Simple! By using web service for binding more data in GridView. So, we will take a web service, add the reference of that web service at our web page, and create a function in web service for loading more data. Again, don't forget that I am using For Loop and static data but you might be using this method for the database. Let's take a look at the web service web method.

Web Service
  1. [WebMethod]  
  2. public string BindGridView()  
  3. {  
  4.     String data = String.Empty;  
  5.     for (Int32 i = 1; i <= 50; i++)   
  6.     {  
  7.         data = data + "<tr>";  
  8.         data = data + "<td>" + i + "</td>";  
  9.         data = data + "<td>Employee_" + i + "</td>";  
  10.         data = data + "<td>Dot Net Developer</td>";  
  11.         data = data + "<td>3 years</td>";  
  12.         data = data + "</tr>";  
  13.     }  
  14.     return data;  
  15. }  
You can see, here, that I am loading the data in a string and returning that string when this web method is called. For more clear understanding, you can refer to the blog about how to call a Web Service. So, our web method is ready. Now, we just need to call it whenever the scroll bar reaches at the end of page. Here, we will also show a loading gif image at the end, so that the viewer is able to know that data is actually loading here. So, in jQuery, we will do the following coding:
  1. $(document).scroll(function()   
  2. {  
  3.     if ($(window).scrollTop() + $(window).height() == $(document).height())  
  4.     {  
  5.         //if condition is true then you are reached at the bottom of web page  
  6.         $("#loading").show().delay(4000).queue(function()  
  7.         {  
  8.             CallWebMethodForMoreRecords();  
  9.             $(this).dequeue();  
  10.         });  
  11.     }  
  12. });  
  13.   
  14. function CallWebMethodForMoreRecords()  
  15. {  
  16.     WebService.BindGridView(onSuccess);  
  17. }  
  18.   
  19. function onSuccess(result)  
  20. {  
  21.     if (result != "")  
  22.     {  
  23.         $("#grdlst > tbody").append(result);  
  24.         $("#loading").hide();  
  25.     }  
  26. }  
Now, see the above jQuery code. What we did is, we created a method CallWebMethodForMoreRecords and called the web service Webmethod here. After getting the records from webmethod, we are binding those records into GridView which renders as table in browser window. So, using the GridView id, I append the result in that. Since the result already is in the tabular form, we don't have any problem in the outlook of the page.

Now, the last thing to know is from where we are calling this method. In the code, you can see that we are calling the CallWebMethodForMoreRecords from the loading event, and doing a delay of four seconds. It is because it is loading the data so fast that we don't even realize that the data is loaded. Also, we want to make our web page look good. So, it will show loading for 4 seconds and then it will call the web service Webmethod; that's it. We are done and ready to rock. We will have the output like this when we scroll at the end of the page.
 
 
 
We are done here. I hope you are going to like it. Thanks. "Happy Coding!"


Similar Articles