Hello,
This is my first experience with C# Web Service & AJAX ... I would like to build a page like Facebook that will load the next set of records when we scroll down ...
I'm trying to use the code but it's not working at all ... Can you help me ?
Thank you
Paul
HTML Code
Page Language="C#" AutoEventWireup="true" CodeFile="auto-load.aspx.cs" Inherits="auto_load" %>
DOCTYPE html>
AEM - Commentstitle>
link href="Style.css" rel="Stylesheet" />
script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js">script>
var isLoading = false;
var v_page_number = 1, v_page_size = 5;
$(document).ready(function ()
{
loadComments();
$(window).scroll(function ()
{
// Start loading when 200px from the bottom
if ($(window).scrollTop() + $(window).height() > $('#main').height() - 200 && !isLoading)
{
loadComments();
}
});
});
function loadComments()
{
isLoading = true;
$.ajax(
{
type: 'POST',
url: "auto-load.aspx/GetComment",
data: '{ "p_page_number" : ' + v_page_number + ', "p_page_size" : ' + v_page_size + ' }',
//contentType: "application/json; charset=utf-8",
dataType: "xml",
success: OnSuccess,
error: OnError
});
}
//
function OnSuccess(data)
{
ShowXMLData(data);
}
//
function OnError(p_error)
{
if (p_error.status != 200)
{
$("#loading").html('Error:' + p_error.responseText + '
Status: ' + p_error.status);
//alert('Error:' + err.responseText + ' Status: ' + err.status);
}
}
function ShowXMLData(xml)
{
debugger;
try
{
var v_comment = $(xml).find('Table');
for (var i = 0; i v_comment.length; i++)
{
var v_time_log_id = v_comment[i]["time_log_id"];
var v_description = v_comment[i]["description"];
var v_hours = v_comment[i]["hours"];
//
var v_comment = $('');
var v_h2_description = $('');
var v_span_hours = $('');
v_h2_description.html(v_description + ' / Id = ' + v_time_log_id + '');
v_span_hours.html('Number of hours: ' + v_hours + '');
v_comment.append(v_h2_description);
v_comment.append(v_span_hours);
v_comment.append('
');
$('#feed').append(v_comment);
});
}
catch(err)
{
var txt = "There was an error on this page.\n\n";
txt += "Error description: " + err.description + "\n\n";
txt += "Click OK to continue.\n\n";
alert(txt);
}
}
script>
head>
form id="frmMain" runat="server">
div id="main">
AEM - Commentsh1>
Showing Comments for AEMp>
div id="feed">
div>
div id="loading">
Loading Comments...
div>
div>
form>
body>C# Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LibAEM;
using System.Data.SqlClient;
using System.Web.Services;
public partial class auto_load : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public string GetComment(int p_page_number, int p_page_size)
{
// ------------------------------------------------------------------------------------------
// Variable declaration
// ------------------------------------------------------------------------------------------
string v_connection_string = LibNamtek.CConnection.GetDBConnection(); // Data Source=192.168.2.40;Initial Catalog=AEM;User ID=aem;Password=aem
SqlConnection v_connection = new SqlConnection(v_connection_string);
try
{
// ------------------------------------------------------------------------------------------
// Main program
// ------------------------------------------------------------------------------------------
// Open Connection
v_connection.Open();
SqlCommand cmd = new SqlCommand("pr_get_paging", v_connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@p_page_number", SqlDbType.Int);
cmd.Parameters["@p_page_number"].Value = p_page_number;
cmd.Parameters.Add("@p_page_size", SqlDbType.Int);
cmd.Parameters["@p_page_size"].Value = p_page_size;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet("TimeLog");
da.Fill(ds);
string v1 = ds.GetXml();
return v1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (v_connection != null)
{
v_connection.Close();
}
}
}
}
Here's the content of XML result
xml version="1.0" standalone="yes"?>
TimeLog>
Table>
1time_log_id>
Analysis for C# Project
7hours>
0minutes>
Table>
Table>
2time_log_id>
Create Database Structuredescription>
4hours>
10minutes>
Table>
Table>
4time_log_id>
Create SP to get list of Contacts & Employeesdescription>
3hours>
20minutes>
Table>
Table>
5time_log_id>
Create Web UI with ASP.Netdescription>
2hours>
30minutes>
Table>
Table>
6time_log_id>
Test the programdescription>
1hours>
15minutes>
Table>
TimeLog>

Nitesh KejriwalRank #253 · 9.9K reputationPosted Apr 2, 2013, 9:45 AM EDT
Use Firebug to see whether AJAX is being called and you get any data back or not.
Paul13 reputationPosted Apr 2, 2013, 10:27 AM EDT
Right now, i'm using jquery 1.7.1.
So i would like to know if the version of JQuery might generate these kind of problems ?
Nitesh KejriwalRank #253 · 9.9K reputationPosted Apr 2, 2013, 10:30 AM EDT
Amit ChoudharyRank #145 · 15.8K reputationPosted Apr 2, 2013, 11:33 AM EDT
just add the content type in your jquery ajax object:
contentType:"text/xml"Paul13 reputationPosted Apr 2, 2013, 11:51 AM EDT