访问WebMethod和jQuery中的公共变量

在我的ASP.Net页面中,我在使用jQuery AJAX滚动时从服务器加载数据。 我正在使用这种方法,因为使用AJAX从服务器加载数据将有助于任何应用程序提高其性能,因为第一次加载屏幕上显示的数据,如果需要,将从服务器加载更多数据作为用户滚动。 我使用以下代码:

$(document).ready( function () { $contentLoadTriggered = false; $(window).scroll( function () { if ($(window).scrollTop() >= ($("#wrapperDiv").height() - $(window).height()) && $contentLoadTriggered == false) { //here I want to check for the isReady variable in ViewState $contentLoadTriggered = true; $.ajax({ type: "POST", url: "MyPage.aspx/GetDataFromServer", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", async: true, cache: false, success: function (msg) { $("#wrapperDiv").append(msg.d); $contentLoadTriggered = false; }, error: function (x, e) { alert("The call to the server side failed. " + x.responseText); } }); } }); }); 

 [WebMethod] public static string GetDataFromServer() { string resp = string.Empty; for (int i = 1; i <= 10; i++) { resp += "

" + i + " This content is dynamically appended to the existing content on scrolling.

"; } //if (myConidition) //ViewState["isReady"] = true; return resp; }

在某些时候(当我的条件满足时),我想停止从服务器加载数据。 所以我考虑在ViewState设置一个布尔变量isReady ,然后在jQuery中检查这个变量的值,以确定是否调用WebMethod。 不幸的是,我不能在WebServices中使用ViewState,我也不知道如何在jQuery中访问ViewState。

我可以使用什么作为ViewState的替代品,可以从WebMethod和jQuery访问它?

我能想到的最好方法是发送自定义类对象或字符串[];

 Public class CustomClass { public string HTML { get; set; } public bool Load { get; set; } } [WebMethod()] public static StatusViewModel GetDataFromServer() { // do work return CustomObject; } 

希望能帮助到你。