开发者

How do I feed back current progress of code behind process to UI from jQuery AJAX call

开发者 https://www.devze.com 2022-12-22 13:18 出处:网络
I have a form that is going to be executing a failry long running process. Not just long, but many small steps during an install process. I\'m trying to stay away from using the built in MS AJAX as mu

I have a form that is going to be executing a failry long running process. Not just long, but many small steps during an install process. I'm trying to stay away from using the built in MS AJAX as much as possible in my entire application, but will use it on this page if it's just the easier way to do things.

But what I want is to only one jQuery AJAX call to code behind and have code behind spit out progess as it hits each step. Here is what I've gotten so far. It's just sample code but it's what I'm trying to do.

UI:

<head runat="server">
    <title></title>
    <script type="text/javascript">
        $(function() {
            $(this).find("#submitForm").click(function() {
                RunCodeBehind();
            });
        });

        function RunCodeBehind() {
            $.ajax({
                error: function(msg) { alert(msg) }, 
                type: "POST",
                url: 'Other.aspx/RunLongRunningProcess',
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(responses) {
                    if (responses.d != "") {
                        //Display process in UI
                        var divEvents = document.getElementById("events");
                        divEvents.innerText = divEvents.innerText + "\n" + data;
                    }
                    else {
                        //no response, update as failed
                        var divEvents = document.getElementById("events");
                        divEvents.innerText = divEvents.innerText + "No response from code behind";
                    }
                }
            });
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <a id="submitForm">Start Process</a>
        <br />
        <div id="events"></div>
    </div>
    </form>
</body>

Code behind:

[WebMethod]
public static string RunLongRunningProcess()
{
    string returnValue;
    var sqlQuery = "SELECT COUNT(*) FROM Users;"; //generic sql query
    returnValue = ExecuteQuery(sqlQuery);

    //feedback cout total to UI, continue process

    var sqlQueryInsert = @"INSERT INTO Log (UserCount) 
                            SELECT COUNT(*) FROM Users;"; //generic sql insert
    returnValue = ExecuteNonQuery(sqlQueryInsert);
//feedback insert step to UI
    var sqlQuery = "SELECT user + ' - ' + name + ' - ' + favday FROM Users;"; //generic sql query
    returnValue = ExecuteQuery(sqlQ开发者_JS百科uery);
    //feedback selection to UI

    return returnValue;
}

Any pointers on how to make it feed back to the UI more than once with just a single call?


I would be inclined to try the other way around, and poll another web service method from your JQuery code instead.

In your 'RunLongRunningProcess' method, I would simply update a session integer variable with values from 0 to 100.

Then I would create a separate web service method that returns the current value of this variable.

You can then poll this new method every (for example) second to get the current upload status.

I believe PeriodicalUpdater for JQuery will allow you to achieve this:

http://www.360innovate.co.uk/blog/2009/03/periodicalupdater-for-jquery/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号