Load Data From Server While Scrolling Using JQuery AJAX
Introduction
This article will demonstrate how to load data from server while scrolling. Loading data from server using ajax will help any application in improving the performance because data which is displayed on the screen is only loaded at first time and more data if required will get loaded from the server as user scroll.
Background
Facebook is the application which inspired me to write the code to load data from server while scrolling. While browsing facebook I was amazed to see that as I scroll on the page new data from the server get appended to the existing one. I became curious to implement the same functionality in C#.Net. I search for the same but did not found any article or blog for doing this stuff in C#.Net. Of course there were few articles for Java and PHP. I read them carefully and started writing the code in C#.Net. As it works successfully I thought to share it, so I am posting it here.
The code
To implement the load on scroll functionality we will require very fewer lines of code. A WebMethod that will be called from client side which returns the content to append while scrolling and a client side function (document.ready) where we will bind scroll event to load data from server. Let us see these server and client side methods in detail.
Server Method: This method is used to get data from database or any source and prepare HTML string depending on the control you are using for appending data to. Here I have just added a message with serial number.
[WebMethod]
public static string GetDataFromServer()
{
string resp = string.Empty;
for(int i = 0; i <= 10; i++)
{
resp += "<p><span>" + counter++ + "</span> This content is dynamically appended to the existing content on scrolling.</p>" ;
}
return resp;
}
Client Method: At client side I have used document.ready method in which I have registered event binding for scroll on div. I have used two div elements one is mainDiv and other one is wrapperDiv. I have registered scroll event for mainDiv and append dynamic data to wrapperDiv.
$(document).ready(
<pre>function()
{
$contentLoadTriggered = false;
$("#mainDiv").scroll(
function()
{
if($("#mainDiv").scrollTop() >= ($("#wrapperDiv").height() - $("#mainDiv").height()) &&
$contentLoadTriggered == false)
$contentLoadTriggered == false)
{
$contentLoadTriggered = true;
$.ajax(
{
type: "POST",
url: "LoadOnScroll.aspx/GetDataFromServer",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg)
{
$("#wrapperDiv").append(msg.d);
$contentLoadTriggered = false;
},
error: function (x, e)
{
alert("The call to the server side failed. " +
x.responseText);
}
}
);
}
}
);
}
);
Here, to check whether scroll is moved at the end following condition is used.
if($("#mainDiv").scrollTop() >= ($("#wrapperDiv").height() - $("#mainDiv").height()) &&
$contentLoadTriggered == false)
This condition will identify whether scroll is moved at the en or not. If it is moved at the end dynamic data will get loaded from the server and get appended to wrapperDiv. Client script to append data to desired div element is written in the script to run when asynchronous call results in success.
success: function (msg)
{
$("#wrapperDiv").append(msg.d);
$contentLoadTriggered = false;
}
Here one thing you will notice that request will
go to the server only when user has scrolled at the end.
I have attached the sample application with this article.
Output
History
First version.
Post Comment
BO5gOF When Someone googles something that relates to one of my wordpress blogs how can I get it to appear on the first page of their serach results?? Thanks!.
EnrCwH Simply want to say your article is as surprising. The clarity in
TyfCIZ Wow, fantastic weblog structure! How long have you ever been running a blog for? you make blogging glance easy. The entire glance of your website is great, let alone the content!
Et08zV Awesome article post.Really looking forward to read more. Cool.
vf7Ds0 I will immediately clutch your rss feed as I can't to find your e-mail subscription link or newsletter service. Do you've any? Kindly let me understand so that I could subscribe. Thanks.
LVH8bO Enjoyed every bit of your blog post.Really thank you! Really Great.
18pNxe Thanks for the article post.Really thank you! Really Great.
JCaBlq Major thankies for the blog.Much thanks again. Great.
cUvmKM This is one awesome blog post. Keep writing.
PuvSrG Really enjoyed this post.Really thank you! Will read on...
pm1RhY Looking forward to reading more. Great blog.Much thanks again. Really Great.
R9vcwH Thanks for sharing, this is a fantastic blog post. Great.
99coA4 wow, awesome blog post.Really looking forward to read more. Really Great.
9NiDV8 Thanks for the blog post.Really thank you! Much obliged.
qJoAK1 Im thankful for the blog article.Much thanks again. Great.
FuCqsD I really enjoy the article.Really looking forward to read more. Will read on...
D07jIU Thank you for your blog.Really looking forward to read more. Cool.
va4IT8 Really appreciate you sharing this blog.Really looking forward to read more. Keep writing.
PW0aqc A big thank you for your article post. Really Cool.