Check what control initiated AJAX Request

北城以北 提交于 2019-12-03 08:55:08

This is what I am doing in my code to identify what control has initialized the request. All javascript code.

function pageLoad() {
    if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
        Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initializeRequest);
    }
}
function endRequestHandler(sender, args) {
    if (sender._postBackSettings.sourceElement.id == '<%= gvResults.ClientID %>')        {
        //do something because of this...
    }
}
function initializeRequest(sender, args) {
    if (CheckForSessionTimeout()) {
        args.set_cancel(true);
    }
    else {
        if (sender._postBackSettings.sourceElement.id == '<%= gvResults.ClientID %>') {
             //do something because of this
        }
    }
}

EDIT
Here is the method of checking for timeout on the client side.

var sessionTimeoutDateTime = new Date();
    var sessionTimeoutInterval = <%= this.SesstionTimeoutMinutes %>;

    function CheckForSessionTimeout() {
        var currentDateTime = new Date()
        var iMiliSeconds = (currentDateTime - sessionTimeoutDateTime);
        if (iMiliSeconds >= sessionTimeoutInterval) {
            ShowSessionTimeout();
            return true;
        }
        return false;
    }

I would recommend that you do not have each control execute the same javascript function. OR, if they do, pass a parameter that indicates which one executed it.

Then, you can include your ajax in the js function that the control executes.

And, if I'm not understanding the issue correctly, perhaps you could explain it in more detail or post some code.

I've read some documentation, and turns out you CAN check the "sender" control. JS in the question is updated to show the proper method signature.

This article gives even better explanation.

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