向下滚动时,asp.net jquery动态加载数据

时间:2013-03-06 18:53:12

标签: jquery asp.net session

向下滚动到底部时,我正在尝试将新项目加载到电子商务网站。我正在做大部分工作,但它得到相同的数据加载...我传递一个计数器(通过会话)来选择新的行,但它不起作用。

这是jquery代码......

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");
        }
    });
}

这是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;
}

我正在尝试增加会话并使用jquery传递它。但它并没有增加会话。如何增加会话数?

1 个答案:

答案 0 :(得分:0)

每次连续调用AJAX Web方法时,此行都不会更改:

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

该行已使用<{1}}中的任何值呈现给。因此,每次AJAX调用触发时,它都会使用与Session["count_of_rows_displayed"]相同的原始值。

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

这样的事情:

Session["count_of_rows_displayed"]

更新:您说网络方法无法访问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:)

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

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

Session
相关问题