How to wait on the __doPostBack method to complete in javascript?

人盡茶涼 提交于 2019-12-03 08:44:32

问题


I am using the __doPostBack method to refresh the UpdatePanel in javascript. I just want to wait for the update panel to get updated and then execute the subsequent javascript code. How do I wait on an asynchronous method to complete before I proceed further with execution (like in my case the asynchronous method is __doPostBack)? I want to simulate something like the way it is doing in C# using Thread.Join() method.


回答1:


Use the Sys.WebForms.PageRequestManager endRequest event:

<script type="text/javascript" language="javascript">
    function clearPostBack() {
        $get('__EVENTTARGET').value = $get('__EVENTARGUMENT').value = '';
        Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(clearPostBack);

        // TODO: anything you want after __doPostBack
    }

    function myPostBack(eventTargetUniqueId, eventArgument) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(clearPostBack);
        __doPostBack(eventTargetUniqueId, eventArgument);
    }
</script>



回答2:


Here is a much better way, which allows you to do something in case of successfull request and/or error:

$.ajax({
        type: "POST",
        data: 
        {
            '__EVENTTARGET': '<%=YourButtonName.ClientID %>'
        }
    }).done(function (data) {
        alert("ok");
    }).fail(function (p1, p2, p3) {
        alert("error");
    });


来源:https://stackoverflow.com/questions/6504472/how-to-wait-on-the-dopostback-method-to-complete-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!