【问题标题】:asp.net jquery load data dynamically when scroll down向下滚动时,asp.net jquery 动态加载数据
【发布时间】:2013-03-07 03:11:32
【问题描述】:

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

这里是 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]
 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 传递它。但它不会增加会话。如何增加会话?

【问题讨论】:

标签: jquery asp.net session


【解决方案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 的那一行。所以不要依赖Session 来存储这个值,你应该把它渲染成一个 JavaScript 值,然后从那个值开始操作。像这样的:

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

【讨论】:

  • 但是由于getdata()是webmethod,所以无法访问Session。这就是为什么我想做点什么......
  • 不能吗?哎呀。嗯,我有个主意。我会用它来更新答案。敬请期待……
  • 我现在正在尝试使用隐藏字段
  • @Borsel:答案已更新。隐藏字段也有效,是的。关键是简单地在客户端存储值并完全删除 Session 依赖项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
相关资源
最近更新 更多