我正在尝试使用 javascript 和以下 sql 语句(sql server 2008)在我的 .NET 网站中进行无限滚动。此 sql 获取前 10 行,但我的 javascript 导致每次用户滚动到页面底部时执行 sql,并且每次它都会拉取相同的(前 10 条)记录,但我希望它拉下 NEXT 10 条记录,每次用户滚动到底部。每次用户滚动到页面底部时,如何使用此 sql 和 row_number 来获取 NEXT 10 行?
SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY DateTime) As RowNum,
* From Topic) As a
WHERE RowNum
BETWEEN 1 AND 10
这是javascript:
<script type="text/javascript">
$(document).ready(function () {
function lastPostFunc() {
$('#divPostsLoader').html('<img src="images/bigLoader.gif">');
//send a query to server side to present new content
$.ajax({
type: "POST",
url: "Default.aspx/Foo",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data != "") {
$('.divLoadData:last').after(data.d);
}
$('#divPostsLoader').empty();
}
})
};
//When scroll down, the scroller is at the bottom with the function below and fire the lastPostFunc function
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
lastPostFunc();
}
});
});
</script>
我现在在存储过程中有上面的 sql:
<System.Web.Services.WebMethod()>
Public Shared Function Foo() As String
Dim strConn As String = "Data Source="
Dim conn As New SqlConnection(strConn)
Dim Cmd As New SqlCommand("InfiniteScroll", conn)
Cmd.CommandType = CommandType.StoredProcedure
Dim DA As New SqlDataAdapter(Cmd)
Dim DS As New DataSet()
DA.Fill(DS, "RefologyForumTopic")
Dim getPostsText As New StringBuilder()
Dim dv As DataView = DS.Tables(0).DefaultView
For Each myDataRow As DataRowView In dv
getPostsText.AppendFormat("price: {0}</br>", myDataRow("Topic"))
getPostsText.AppendFormat("description: {0}</br></p>", myDataRow("UserID"))
Next
getPostsText.AppendFormat("<div style='height:15px;'></div>")
Return getPostsText.ToString()
End Function