views:

25

answers:

0

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(sqlQuery);
    //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?