I am trying to Load list items in lazy load like Facebook. I have a grid that loads data from a list, and that list may have up to 5000 items and to load all those Items in grid it takes a lot of time so performance is key on my scenario. I could have used gridview paging but this solution was canceled as not the proper solution. Instead I have to load data when I scroll down the scroller till the last item is being loaded.
So I have the following code but I am getting this error: Failed to load resource: the server responded with a status of 500 (Internal Server Error)
<script type="text/javascript">
var pageIndex = 1;
var pageCount;
$(function () {
//Remove the original GridView header
$("[id$=gvCustomers] tr").eq(0).remove();
});
//Load GridView Rows when DIV is scrolled
$("#dvGrid").on("scroll", function (e) {
var $o = $(e.currentTarget);
if ($o[0].scrollHeight - $o.scrollTop() <= $o.outerHeight()) {
GetRecords();
}
});
//Function to make AJAX call to the Web Method
function GetRecords() {
pageIndex ++;
//if (pageIndex == 2 || pageIndex <= pageCount)
{
$.ajax({
type: "POST",
url: "TESTLargeData.aspx/GetCustomers",
data: '{pageIndex: ' + pageIndex + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (xhr, status, error) {
alert(error);
}
});
}
}
//Function to recieve XML response append rows to GridView
function OnSuccess(response) {
alert('onsuces!');
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
pageCount = parseInt(xml.find("PageCount").eq(0).find("PageCount").text());
var customers = xml.find("Customers");
$("[id$=gvCustomers] .loader").remove();
customers.each(function () {
var customer = $(this);
var row = $("[id$=gvCustomers] tr").eq(0).clone(true);
$(".Id", row).html(customer.find("Id").text());
$(".Name", row).html(customer.find("Name").text());
$("[id$=gvCustomers]").append(row);
});
//Hide Loader
$("#loader").hide();
}
C# code
protected void Page_Load(object sender, EventArgs e)
{
gvCustomers.DataSource = FetchList(30);
gvCustomers.DataBind();
}
private static DataSet FetchList(int pageIndex)
{
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(string));
dt.Columns.Add("Name", typeof(string));
SPList lista = SPContext.Current.Web.GetList("mysite/Lists/LageData");
SPListItemCollection items = lista.GetItems();
for (int i = pageIndex; i < pageIndex +5; i++)
{
DataRow dr = dt.NewRow();
dr["Id"] = items[i]["ID"].ToString();
dr["Name"] = items[i]["Title"].ToString();
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
return ds;
}
}
[WebMethod]
public static string GetCustomers(int pageIndex)
{
return FetchList(pageIndex).GetXml();
}
FetchList
method and return the catch stack trace to see what exactly is the error?Internal Server Error