none
Ajax页面中,__doPostBack是如何判读何时发出异步请求的? RRS feed

  • 问题

  • HI,我的页面上使用了UpdatePanel,同时我需要使用脚本调用__doPostBack向服务器发送命令,如:__doPostBack("Delete","2").

    一切工作正常,但有时doPostBack是异步请求,有时又是页面回发。请问哪位知道Ajax页面中,__doPostBack是如何判读何时发出异步请求的?

    2011年2月18日 3:21

答案

  • Performing async postback from javascript

    UpdatePanels are a great way to update portions of a webpage. They only have one "problem": they need a control to fire the async postback. When you are performing a postback from javascript using the following code:

    __doPostBack( 'EventName''EventArgs' );

    the execution will always be synchronous. You can work around this by using a hidden control and fire one of its events, but this is not always the most flexible solution. I debugged the framework javascript a little and found another way to do it, completely in javascript. It's done by adding the eventtargetname into two arrays of the PageRequestManager. I have created a javascript function that handles this for me:

    function doPostBackAsync( eventName, eventArgs )
    {
        var prm = Sys.WebForms.PageRequestManager.getInstance();

        if ( !Array.contains( prm._asyncPostBackControlIDs, eventName) )
        {
            prm._asyncPostBackControlIDs.push(eventName);
        }
     
        if ( !Array.contains( prm._asyncPostBackControlClientIDs, eventName) )
        {
            prm._asyncPostBackControlClientIDs.push(eventName);
        }
     
        __doPostBack( eventName, eventArgs );
    }

    You can call it just like the original __doPostBack function.

    • 已标记为答案 Alex11986 2011年2月18日 4:14
    2011年2月18日 4:14