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

此行不会随着对 AJAX Web 方法的每次连续调用而改变:

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

该行以当时的任何值呈现到页面一次Session["count_of_rows_displayed"]。因此,每次 AJAX 调用触发时,它都会使用相同的原始值number_of_rows.

由于您在Session对象中跟踪此服务器端,因此您可能不需要从客户端传递值。只需Session["count_of_rows_displayed"]在服务器端 Web 方法中直接引用并增加其值即可。

像这样的东西:

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
}

更新:您说网络方法无法访问Session?好吧,我想我还没有遇到过,因为我还没有发现需要WebMethod并且我虔诚地尽量避免使用Session:)

在这种情况下,这可能是总体上更好的方法,您仍然可以使用您预期的 RESTful 方法,每次将值传递给 Web 方法。但是,您还需要在客户端增加该值。

您所犯的最初错误是认为代码将重新渲染Session多次引用的那一行。因此,您应该将其呈现为 JavaScript 值,然后根据该值进行操作,而不是依赖于Session存储此值。像这样的东西:

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 回答