检查什么控件发起了AJAX请求

发布于 2024-07-14 06:12:18 字数 982 浏览 5 评论 0原文

asp.net 2.0 / jQuery / AJAX

<script type="text/javascript">
//updated to show proper method signature

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(hideMessage);

function hideMessage(sender, args)
{
    var ctl = args.get_postBackElement();
    //check if ctl is the disired control
    //hide user notification message
}
</script>

我在页面上有几个可能发起 AJAX 请求的控件,但我只希望当我单击一个特定按钮时触发我的 js。 我如何检查哪个控件发起了请求,以便我可以相应地触发 JS。

编辑:我解决了这个问题,但我仍然想知道我是否可以这样做。

说明:我无法从 onclick 事件调用 JS,因为该页面位于 UpdatePanel 内部,并且我只希望 JS 在 AJAX 请求结束时执行,并且由上的一个特定按钮触发这一页。 在服务器端,我将 myLabel.Text 设置为一些文本,然后 js 检查 $(myLabel.CliendID) 的 innerHTML 是否不为空并触发 js。 检查innerHTML是我的解决方法,因为我不知道如何检查AJAX请求的“发送者”。 希望现在这更有意义。

edit2:我读过一些文档,事实证明您可以检查“发送者”控件。

谢谢。

asp.net 2.0 / jQuery / AJAX

<script type="text/javascript">
//updated to show proper method signature

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(hideMessage);

function hideMessage(sender, args)
{
    var ctl = args.get_postBackElement();
    //check if ctl is the disired control
    //hide user notification message
}
</script>

i have several controls on the page that might initiate the AJAX request, but i only want my js to fire when i click one particular button. how do i check what control initiated the request so i can fire JS accordingly.

EDIT: I worked around it, but I'd still like to know if I can do it this way.

Clarification: I can't call the JS from onclick event, because the page is inside of the UpdatePanel, and i only want the JS to execute when AJAX Request ends and it was triggered by one particular button on the page. On server side, i set the myLabel.Text to some text, and then js checks if the $(myLabel.CliendID)'s innerHTML is not blank and fires the js. checking the innerHTML is my work-around since i can't figure out how to check the "sender" of AJAX Request. Hope this makes more sense now.

edit2: I've read some documentation, and turns out you CAN check the "sender" control.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎歌 2024-07-21 06:12:18

这就是我在代码中所做的,以识别哪个控件初始化了请求。 所有 JavaScript 代码。

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
        }
    }
}

编辑
这是在客户端检查超时的方法。

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;
    }

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;
    }
月野兔 2024-07-21 06:12:18

我建议您不要让每个控件执行相同的 javascript 函数。 或者,如果它们这样做,则传递一个参数来指示是哪一个执行了它。

然后,您可以将 ajax 包含在控件执行的 js 函数中。

而且,如果我没有正确理解这个问题,也许你可以更详细地解释它或发布一些代码。

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.

紧拥背影 2024-07-21 06:12:18

我读过一些文档,结果你可以检查“发送者”控制。 问题中的 JS 已更新以显示正确的方法签名。

这篇文章提供了更好的解释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文