A day with .Net

My day to day experince in .net

Load data dynamically on page scroll using jquery,ajax and asp.net.

Posted by vivekcek on March 11, 2012

Introduction

In Facebook you might saw status updates of your friends are dynamically loaded when you scroll the browser’s scroll bar. So in this article i am going to explain how we can achieve this using jQuery and Ajax.

Using the code

The solution includes 2 web forms(Default.aspx and AjaxProcess.aspx). The Default.aspx contain a Div with ID “myDiv“. Initially the Div contain some static data, then data is dynamically appended to the Div 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 invoke 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 at 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 at the bottom or not. If it has moved at the bottom, dynamic data will get loaded from the server and get appended to myDiv.

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

Download Code

https://skydrive.live.com/redir.aspx?cid=12c9d813342f227a&resid=12C9D813342F227A!126&parid=12C9D813342F227A!120&authkey=!AJIbc1495KJup70

Advertisements

3 Responses to “Load data dynamically on page scroll using jquery,ajax and asp.net.”

  1. Thank you so much…Its helpful.

  2. yeluri said

    Hi Vivek,

    Thanks for your share. Please tell me if I want to load content from my database (mysql)

  3. Rizwan Khan said

    thanks its very helpful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s