implmented lazy loading when the window is scolled .
here is the code :-
Javascript:
$(document).ready(function () {
function lastPostFunc() {
$('#divPostsLoader').html('<img src="images/bigLoader.gif">');
//send a query to server side to present new
content
$.ajax({
type:
"POST",
url: "Default3.aspx/GetDataFromServer",
data: "{}",
contentType: "application/json;
charset=utf-8",
dataType: "json",
success: function (data) {
if (data != "")
{
$("#dd").html(data.d);
}
$('#divPostsLoader').empty();
}
})
};
$(window).scroll(function () {
if ($(window).scrollTop() ==
$(document).height() - $(window).height()) {
lastPostFunc();
}
});
});
Code behind :
static public int initialRecord = 2;
static public int incrementRecord = 2;
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
initialRecord = 2;
ShowData();
}
}
[WebMethod]
public static string GetDataFromServer()
{
SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
DataTable dt = new
DataTable();
using (SqlCommand
sqlCommand = new SqlCommand("select Top(" + initialRecord + ") * from Image", sqlConnection))
{
SqlDataAdapter sqldataAdapter = new SqlDataAdapter(sqlCommand);
sqldataAdapter.Fill(dt);
}
string str = "";
for (int i = 0;
i < dt.Rows.Count; i++)
{
str += "<li><img
src='image/MidleImages/" + dt.Rows[i]["image"]
+ "' width='580' height='360' alt='Grass
Blades' /></li>";
}
initialRecord += 2;
return str;
}
public void ShowData()
{
SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
DataTable dt = new
DataTable();
using (SqlCommand
sqlCommand = new SqlCommand("select Top(" + initialRecord + ") * from Image", sqlConnection))
{
SqlDataAdapter sqldataAdapter = new SqlDataAdapter(sqlCommand);
sqldataAdapter.Fill(dt);
}
string str = "";
for (int i = 0;
i < dt.Rows.Count; i++)
{
str += "<li><img
src='image/MidleImages/" + dt.Rows[i]["image"]
+ "' width='580' height='360' alt='Grass
Blades' /></li>";
}
initialRecord += 2;
dd.InnerHtml = str;
}
this will work ...........
No comments:
Post a Comment