2013-03-06 65 views
0

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

这里是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传递。但它不增加会话。我怎样才能让会话增加?

+0

这或许可以帮助你... http://www.aspsnippets.com/Articles/Load-data-while-Scrolling-Page-down-with-jQuery-AJAX-and- ASPNet.aspx – Arbaaz 2013-03-06 19:11:18

回答

0

此行不为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 
} 

更新:你说,Web方法不能访问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"); 
     } 
    }); 
} 
+0

但由于getdata()是web方法,因此无法访问Session。这就是为什么我试图做一些事情...... – 2013-03-06 19:04:06

+0

它不能?吉兹。那么,我有一个想法。我会用它更新答案。敬请期待... – David 2013-03-06 19:05:28

+0

我正在尝试使用隐藏字段 – 2013-03-06 19:07:10