检查什么样的控制发起AJAX请求 [英] Check what control initiated AJAX Request
问题描述
asp.net 2.0 / jQuery的/ AJAX
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。
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.
编辑:我的工作围绕它,但我仍想知道如果我能做到这样
I worked around it, but I'd still like to know if I can do it this way.
澄清:我无法从onclick事件调用JS,因为页面是在UpdatePanel里面,我只希望JS的当AJAX请求终止执行,它是由一个触发在页面上特定按钮。在服务器端,我设置了myLabel.Text一些文本,然后JS检查是否$(myLabel.CliendID)的innerHTML的不是空白,并触发JS。检查的innerHTML是我的解决方法,因为我无法弄清楚如何检查AJAX请求的发件人。希望这会更有意义吧。
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:我读过一些文档< /一>,并证明你可以检查发件人的控制。
edit2: I've read some documentation, and turns out you CAN check the "sender" control.
感谢你。
推荐答案
这是我在做什么在我的code,以确定哪些控制已初始化请求。所有的JavaScript code。
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;
}
这篇关于检查什么样的控制发起AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!