Click here to Skip to main content
Click here to Skip to main content

Load data dynamically on page scroll using jQuery, AJAX, and ASP.NET

By , 6 Sep 2012
 

Introduction

In Facebook you might see status updates of your friends dynamically loaded when you scroll the browser’s scroll bar. In this article I am going to explain how we can achieve this using jQuery and AJAX. 

Using the code

The solution includes two web forms (Default.aspx and AjaxProcess.aspx). The Default.aspx contains a Div with ID myDiv. Initially the Div contains some static data, then data is dynamically appended to it using jQuery and AJAX. 

<div id="myDiv">
	<p>Static data initially rendered.</p>
</div>

The second Web Form AjaxProcess.aspx contains a web method GetData() that is called using AJAX to retrieve data.

[WebMethod]
public static string GetData()
{
    string resp = string.Empty;
    resp += "<p>This content is dynamically appended to the existing content on scrolling.</p>";
    return resp;
}

Now we can add some jQuery script in Default.aspx that will be fired on page scroll and invokes the GetData()method.

$(document).ready(function () {

   $(window).scroll(function () {
       if ($(window).scrollTop() == $(document).height() - $(window).height()) {
           sendData();
       }
   });

   function sendData() {
       $.ajax(
        {
            type: "POST",
            url: "AjaxProcess.aspx/GetData",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: "true",
            cache: "false",

            success: function (msg) {
                $("#myDiv").append(msg.d);
            },

            Error: function (x, e) {
                alert("Some error");
            }
        });
   }
});

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

$(window).scroll(function () {
   if ($(window).scrollTop() == $(document).height() - $(window).height()) {
       sendData();
   }
});

This condition will identify whether the scroll has moved to the bottom or not. If it has moved to the bottom, dynamic data will get loaded from the server and get appended to myDiv.

success: function (msg) {
    $("#myDiv").append(msg.d);
},

History

Version 1.

License

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

About the Author

vivek poomala
Web Developer Teknokraaft info systems
India India
Member
http://vivekcek.wordpress.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberbhargavpp17 Sep '12 - 19:36 
i have some basic idea of it but how to calculate i dont know
Question[My vote of 1] kill me with a blunt knife, please...memberSeishin#13 Mar '12 - 22:19 
string resp = string.Empty;
resp += "<p>This content is dynamically appended to the existing content on scrolling.</p>";
=_='
 

that aside, data loaded in this manner is problably dependant on what you have already loaded ex. blog entries older that what you currently display, so you should show how to store, retreive and pass parameters.
 
also if you just fetch data why don't you use $.get? much simpler..
life is study!!!

AnswerRe: [My vote of 1] kill me with a blunt knife, please...membervivek poomala14 Mar '12 - 1:16 
take it as sample you can write mechanism for passing some parameters,counters etc by using static variables
GeneralRe: [My vote of 1] kill me with a blunt knife, please...memberDewey10 Sep '12 - 7:03 
Take it as a sample???
 
Really???
 
You don't even post a link!
 
Fail!
AnswerRe: [My vote of 1] kill me with a blunt knife, please...memberkomaliShiru1 Aug '12 - 21:44 
Hi ,
Can you please post the whole code so that i can use it for my project.
 

 
Thanks...

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 6 Sep 2012
Article Copyright 2012 by vivek poomala
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid