Click here to Skip to main content
13,350,822 members (56,695 online)
Click here to Skip to main content
Add your own
alternative version


146 bookmarked
Posted 11 Aug 2011

Load Data From Server While Scrolling Using jQuery AJAX

, 26 Apr 2012
Rate this:
Please Sign up or sign in to vote.
This article explains how to implement on-scroll loading data from the server using jQuery as seen on Facebook.


This article will demonstrate how to load data from the server while scrolling. Loading data from the server using AJX will help any application in improving its performance because data which is displayed on the screen alone is loaded the first time and more data, if required, will get loaded from the server as the user scrolls.


Facebook is the application which inspired me to write code to load data from the server while scrolling. While browsing Facebook, I was amazed to see that as I scrolled on the page, new data from the server got appended to the existing one. I became curious about implementing the same functionality in C#. I searched for information, but did not find any article or blog on doing this in C#. Of course, there were a few articles for Java and PHP. I read them carefully and started writing code in C#. As it worked successfully I thought I'd share it, so I am posting it here.

The code

To implement the load on scroll functionality, we will require very few lines of code. A WebMethod that will be called from the client side which returns the content to append while scrolling, and a client side function (document.ready) where we will bind the scroll event to load data from the server. Let us see these server and client side methods in detail.

Server method: This method is used to get data from the database or any source and prepares an HTML string depending on the control you are using for appending data to. Here I have just added a message with the serial number.

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; 

If you want to load data from database you can modify your method as follows : 

public static string GetDataFromServer()
        DataSet ds = new DataSet();
        // Set value of connection string here
        string strConnectionString = ""; // Insert your connection string value here
        SqlConnection con = new SqlConnection(strConnectionString);
        // Write the select command value as first parameter
        SqlCommand command = new SqlCommand("SELECT * FROM Person", con);
        SqlDataAdapter adp = new SqlDataAdapter(command);
int retVal = adp.Fill(ds);
        string resp = string.Empty;
for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
            string strComment = string.Empty;
if (ds.Tables != null)
if (ds.Tables[0] != null)
if (ds.Tables[0].Rows.Count > 0)
if (ds.Tables[0].Rows.Count >= i - 1)
if (ds.Tables[0].Rows[i - 1][0] != DBNull.Value)
                                strComment = ds.Tables[0].Rows[i - 1][0].ToString();
            resp += "<p><span>" + counter++ + "</span> " + strComment + "</p>";
return resp;

Client method: At the client side, I have used the document.ready method in which I have registered event binding for scroll on div. I have used two div elements: mainDiv and wrapperDiv. I have registered the scroll event for mainDiv and append dynamic data to wrapperDiv.

$contentLoadTriggered = false;
if($("#mainDiv").scrollTop() >= ($("#wrapperDiv").height() - 
   $("#mainDiv").height()) &&
   $contentLoadTriggered == false)
   $contentLoadTriggered == false)
$contentLoadTriggered = true;
type: "POST",
url: "LoadOnScroll.aspx/GetDataFromServer",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) 
$contentLoadTriggered = false;
error: function (x, e)
alert("The call to the server side failed. " +

Here, to check whether the scroller has moved at the bottom, the following condition is used.

if($("#mainDiv").scrollTop() >= 
  ($("#wrapperDiv").height() - $("#mainDiv").height()) &&
   $contentLoadTriggered == false)

This condition will identify whether the scroller has moved at the bottom or not. If it has moved at the bottom, dynamic data will get loaded from the server and get appended to wrapperDiv. The client script to append data to the desired div element is written in the script to run when the asynchronous call results in success.

success: function (msg)
$contentLoadTriggered = false;

Here one thing you will notice is that the request will go to the server only when the user has scrolled at the bottom.

I have attached the sample application with this article.





First version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Praveen Meghwal
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Generalits very usefull Pin
suraj mahajan27-Mar-12 4:06
membersuraj mahajan27-Mar-12 4:06 
GeneralRe: its very usefull Pin
Praveen Meghwal27-Mar-12 20:23
memberPraveen Meghwal27-Mar-12 20:23 
QuestionMy vote of 5 Pin
Nguyen Quy Minh22-Feb-12 6:10
memberNguyen Quy Minh22-Feb-12 6:10 
AnswerRe: My vote of 5 Pin
Praveen Meghwal22-Feb-12 21:05
memberPraveen Meghwal22-Feb-12 21:05 
QuestionThanks for sharing such a nice article. Pin
Zaheer Mazhar5-Jan-12 6:37
memberZaheer Mazhar5-Jan-12 6:37 
AnswerRe: Thanks for sharing such a nice article. Pin
Zaheer Mazhar5-Jan-12 7:22
memberZaheer Mazhar5-Jan-12 7:22 
GeneralRe: Thanks for sharing such a nice article. Pin
Praveen Meghwal8-Jan-12 23:56
memberPraveen Meghwal8-Jan-12 23:56 
QuestionAdding SQL data Pin
Damo B13-Dec-11 6:46
memberDamo B13-Dec-11 6:46 
Hi, do you have any sample code for implementing a call to get SQL data?

This is what I have so far:

<WebMethod()> _
Public shared Function GetDataFromServer() As String
    Dim resp As String = String.Empty

    Dim dv As DataView = CType(GetNews.Select(DataSourceSelectArguments.Empty), DataView)

    For Each dr As DataRow In dv.Table.Rows
        resp += dr("title").ToString() & "<br />"

    Return resp
End Function

but obviously I get the
cannot refer to an instance member of a class from within a shared method

error when trying to get data from the sqldatasource.

I would really appreciate a code snippet to show how to implement sql data.

AnswerRe: Adding SQL data Pin
Praveen Meghwal8-Jan-12 23:59
memberPraveen Meghwal8-Jan-12 23:59 
GeneralMy vote of 3 Pin
basskaran27-Nov-11 23:26
memberbasskaran27-Nov-11 23:26 
GeneralRe: My vote of 3 Pin
Praveen Meghwal15-Dec-11 22:17
memberPraveen Meghwal15-Dec-11 22:17 
GeneralMy vote of 5 Pin
mhamad zarif29-Oct-11 8:46
membermhamad zarif29-Oct-11 8:46 
GeneralRe: My vote of 5 Pin
Praveen Meghwal15-Dec-11 22:17
memberPraveen Meghwal15-Dec-11 22:17 
Questionmy vote of 5... Pin
Kyaw Aung Win24-Oct-11 23:43
memberKyaw Aung Win24-Oct-11 23:43 
AnswerRe: my vote of 5... Pin
Praveen Meghwal15-Dec-11 22:16
memberPraveen Meghwal15-Dec-11 22:16 
GeneralMy vote of 5 Pin
Dilip Baboo18-Aug-11 9:15
memberDilip Baboo18-Aug-11 9:15 
GeneralRe: My vote of 5 Pin
Praveen Meghwal18-Aug-11 22:03
memberPraveen Meghwal18-Aug-11 22:03 
GeneralMy vote of 5 Pin
b.paseban17-Aug-11 10:22
memberb.paseban17-Aug-11 10:22 
GeneralRe: My vote of 5 Pin
Praveen Meghwal17-Aug-11 21:26
memberPraveen Meghwal17-Aug-11 21:26 
Generalnice article ..would try it with JSP though Pin
acnielson16-Aug-11 21:58
memberacnielson16-Aug-11 21:58 
GeneralRe: nice article ..would try it with JSP though Pin
Praveen Meghwal16-Aug-11 23:22
memberPraveen Meghwal16-Aug-11 23:22 
GeneralMy vote of 5 Pin
Member 57349116-Aug-11 19:29
memberMember 57349116-Aug-11 19:29 
GeneralRe: My vote of 5 Pin
Praveen Meghwal17-Aug-11 0:11
memberPraveen Meghwal17-Aug-11 0:11 
GeneralMy vote of 5 Pin
JeanMichelG15-Aug-11 19:55
memberJeanMichelG15-Aug-11 19:55 
GeneralRe: My vote of 5 Pin
Praveen Meghwal15-Aug-11 22:14
memberPraveen Meghwal15-Aug-11 22:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 27 Apr 2012
Article Copyright 2011 by Praveen Meghwal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid