在访问的WebMethod和jQuery公共变量 [英] Accessing a common variable in WebMethod and jQuery

查看:121
本文介绍了在访问的WebMethod和jQuery公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.Net页面,我从服务器在使用jQuery AJAX滚动加载数据。我使用以来使用AJAX将有助于改善其性能的任何应用程序,因为它是单独显示在屏幕上的数据被装载在第一时间和更多的数据从服务器加载数据该方法中,如果需要的话,将得到从服务器作为加载用户滚动。我使用下面的code:

In my ASP.Net page, I am loading data from server while scrolling using jQuery AJAX. I am using this method since loading data from the server using AJAX will help any application in improving its performance because data which is displayed on the screen alone is loaded the first time and more data, if required, will get loaded from the server as the user scrolls. I am using the following code:

$(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 += "<p><span>" + i + "</span> This content is dynamically appended to the existing content on scrolling.</p>";
    }

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

    return resp;
}

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

At some point (when my condition is met), I want to stop loading data from the server. So I thought about setting a boolean variable isReady in the ViewState and then check the value of this variable in jQuery to determine whether or not to call the WebMethod. Unfortunately, I can't use ViewState in WebServices and I also don't know how to access ViewState in jQuery.

我可以作为替代的ViewState,这可以从以下的WebMethod和jQuery的两个被访问使用?

What can I use as an alternative to ViewState, which can be accessed from both the WebMethod and the jQuery?

推荐答案

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

The best way i can think of is to send a custom class object or string [];

Public class CustomClass
{
    public string HTML { get; set; }
    public bool Load  { get; set; }
}

[WebMethod()]
public static StatusViewModel  GetDataFromServer()
{
    // do work
    return CustomObject;
}

希望它帮助。

这篇关于在访问的WebMethod和jQuery公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆