0

I am tryin to load new items to e-commerce website when scroll down to the bottom. I am doing most part of it but it gets the same data to load... I am passing a counter(via session) to select new rows but it doesnt work.

here is the jquery code...

function sendData() {
<% Session["count_of_rows_displayed"] = Convert.ToInt16(Session["count_of_rows_displayed"].ToString()) + 1; %>
alert('<%= Session["count_of_rows_displayed"].ToString() %>');
$.ajax(
    {
        type: "POST",
        url: "insaat.aspx/GetData",
        data: "{'number_of_rows':'" + <%= Session["count_of_rows_displayed"].ToString() %> +"'}",
        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 is the webmethod

[WebMethod]
 public static string GetData(String number_of_rows)
 {
int no = Convert.ToInt16(number_of_rows);
string resp = string.Empty;
SqlConnection connection = new   SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
connection.Open();
adapter.SelectCommand = new SqlCommand("SELECT TOP " + (no*6) + " * FROM (SELECT TOP " + ((++no) * 6) + " * FROM Product ORDER BY id ASC) t ORDER BY id DESC", connection);
adapter.Fill(ds);
connection.Close();

for (i = 0; i <= ds.Tables[0].Rows.Count - 1 && i < 24; i++)
    // build the data 

connection.Close();
return resp;
}

I am trying to increment session and pass it with jquery. But it doesnt increment the session. How can I get session incremented ?

4

1 に答える 1

1

This line doesn't change with each successive call to the AJAX web method:

data: "{'number_of_rows':'" + <%= Session["count_of_rows_displayed"].ToString() %> +"'}",

That line was rendered to the page once with whatever value was in Session["count_of_rows_displayed"] at that time. So every time the AJAX call fires, it uses the same original value for number_of_rows.

Since you're tracking this server-side in the Session object, you probably don't need to pass the value from the client-side anyway. Just reference Session["count_of_rows_displayed"] and increment its value directly in the server-side web method.

Something like this:

public static string GetData()
{
    string number_of_rows = Session["count_of_rows_displayed"];
    int no = Convert.ToInt16(number_of_rows);
    Session["count_of_rows_displayed"] = no + 1;

    // the rest of the method
}

Update: You say that web methods can't access Session? Well, I guess I haven't encountered that since I haven't found a need for WebMethod and I religiously try to avoid using Session :)

In that case, and this is probably the better approach overall, you can still use your intended RESTful approach of passing the value to the web method each time. However, you'll need to also increment that value on the client-side.

The original mistake you were making was in thinking that the code is going to re-render that line which references Session more than once. So instead of relying on Session to store this value, you should render it to a JavaScript value and then operate from that. Something like this:

var rowCount = <%= Convert.ToInt16(Session["count_of_rows_displayed"].ToString() %>;

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

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

        Error: function (x, e) {
            alert("Some error");
        }
    });
}
于 2013-03-06T18:59:57.320 に答える