开发者

jQuery Submit and the Heisenberg Effect

I am trying to do a form.submit that will call a non-static event on the server side and force a full postback; however, I am encountering an example of the Heisenberg Effect. If I put a breakpoint on line 3 of code below, the code-behind gets the message and the submit works. If I put the breakpoint on line 4 instead, the submit fails. The server code actually gets the event, but the client doesn't seem to want to wait for the server to do its thing. The correct function should be that line 4 should never be executed.

1) function LoginSubmit() {
2)    $("#diagnostics").text("LoginSubmit started");
3)    $("#<%=btnLoginSubmit.ClientID %>").click();
4)    $("#diagnostics").text("LoginSubmit failed");
5)    }

Is there a way for me to wait for the server to get and process the btnLoginSubmit event message?

The code below is called when the user clicks the Logout button on the screen.

   function Logout() {
        $("#loginStatus").text("Logged out");
        $("SPAN[id*='lblLoginStatus']").text("");
        $("SPAN[id*='lblLoggedInUserName']").text("");
        $("#userId").text("");
        $("#menuId").text("");
        $("SPAN[id*='spanBtnLogout']").hide();
        $("SPAN[id*='spanBtnLogin']").css("display:inline;");
        $("SPAN[id*='spanBtnLogin']").show();
        $.ajax({
            type: "POST",
            url: "LoginService.asmx/Logout",
            contentType: "application/json; charset=utf-8",
            data: "{}",
            success: function (data, status) {
开发者_JS百科                $.cookie("LoggedInUserId", null);
                $.cookie("LoggedInMenuId", null);
                $.cookie("LoggedInUserName", null);

                LoginSubmit();  // Force a postback
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("Logout function failed: " + XMLHttpRequest.responseText);
            }
        });
    }


No, that's not possible without using AJAX. And I doubt the submit is failing, I would imagine that its just displaying the message. The program flow doesn't stop because you call a DOM element's click() function.

Update: Try this instead to get the C# button event to fire:

$("#<%=btnLoginSubmit.ClientID %>").trigger('click');


Javascript communicates with the server asynchronously. The script will not wait for the server to finish anything before proceeeding unless you build something in to your code to explicitly do that (e.g. ajax and a callback).

Under what circumstances do you want an error message shown? A POST will always result in the page being redrawn. It's still possible to have code that updates the DOM before that finishes (such as your code does now), but whatever happens on that post, this hunk of code will never know about it... because it will be gone and replaced with something else when the POST completes.

You should have your server code render an error message if the login fails, or use AJAX to update the DOM and do it without posting at all.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜