取消一个正在执行的异步回发
如意编程网
收集整理的这篇文章主要介绍了
取消一个正在执行的异步回发
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
javascript代码:
//pageLoad函数在成功更新后隐藏提示面板
function pageLoad()
{
showmessage("false","");
}
//设定当引发initializeRequest事件时便执行事件处理函数CheckStatus
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);
function CheckStatus(sender,args)
{
//取得PageRequestManager类的实例
var prm=Sys.WebForms.PageRequestManager.getInstance();
//判断是否在进行异步回发且点击了“取消”按钮,就调用PageRequestManager的abortPostBack方法取消异步回发
if(prm.get_isInAsyncPostBack() & args.get_postBackElement().id=='LinkButton1')
{
prm.abortPostBack();
//隐藏提示面板
showmessage("false","");
}
//判断是否在进行异步回发并且又单击了发送异步回发按钮此时取消新的异步回送并显示正在处理先前的请求
else if(prm.get_isInAsyncPostBack() & args.get_postBackElement().id=='Button1')
{
args.set_cancel(true);
showmessage("true","仍在处理先前的请求");
}
//提示正在取得数据
else if(!prm.get_isInAsyncPostBack() & args.get_postBackElement().id=='Button1')
{
showmessage("true","正在取得数据");
}
}
//显示与隐藏提示信息
function showmessage(visiable,msg)
{
if(visiable=="true")
{
var loading=$get("loading");
loading.style.visibility='visible';
$get("message").innerHTML=msg;
}
if(visiable=="false")
{
var loading=$get("loading");
loading.style.visibility='hidden';
$get("message").innerHTML=msg;
}
}
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
前台页面主要代码:
<body>
<form id="form1" runat="server">
<div align="center" >
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Scripts>
<asp:ScriptReference Path="ClientControl.js" />
</Scripts>
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="获取时间" onclick="Button1_Click" />
<br />
<div id="loading" align="left" style=" visibility: hidden;">
<img alt="" src="progressbar_long.gif" /><span id="message"></span>
<asp:LinkButton ID="LinkButton1" runat="server">取消</asp:LinkButton>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<span>当前时间:</span><asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click">
</asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
Button1_Click事件代码:
protected void Button1_Click(object sender, EventArgs e)
{
//刻意暂停6S
System.Threading.Thread.Sleep(6000);
Label1.Text = DateTime.Now.ToString();
}
//pageLoad函数在成功更新后隐藏提示面板
function pageLoad()
{
showmessage("false","");
}
//设定当引发initializeRequest事件时便执行事件处理函数CheckStatus
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);
function CheckStatus(sender,args)
{
//取得PageRequestManager类的实例
var prm=Sys.WebForms.PageRequestManager.getInstance();
//判断是否在进行异步回发且点击了“取消”按钮,就调用PageRequestManager的abortPostBack方法取消异步回发
if(prm.get_isInAsyncPostBack() & args.get_postBackElement().id=='LinkButton1')
{
prm.abortPostBack();
//隐藏提示面板
showmessage("false","");
}
//判断是否在进行异步回发并且又单击了发送异步回发按钮此时取消新的异步回送并显示正在处理先前的请求
else if(prm.get_isInAsyncPostBack() & args.get_postBackElement().id=='Button1')
{
args.set_cancel(true);
showmessage("true","仍在处理先前的请求");
}
//提示正在取得数据
else if(!prm.get_isInAsyncPostBack() & args.get_postBackElement().id=='Button1')
{
showmessage("true","正在取得数据");
}
}
//显示与隐藏提示信息
function showmessage(visiable,msg)
{
if(visiable=="true")
{
var loading=$get("loading");
loading.style.visibility='visible';
$get("message").innerHTML=msg;
}
if(visiable=="false")
{
var loading=$get("loading");
loading.style.visibility='hidden';
$get("message").innerHTML=msg;
}
}
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
前台页面主要代码:
<body>
<form id="form1" runat="server">
<div align="center" >
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Scripts>
<asp:ScriptReference Path="ClientControl.js" />
</Scripts>
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="获取时间" onclick="Button1_Click" />
<br />
<div id="loading" align="left" style=" visibility: hidden;">
<img alt="" src="progressbar_long.gif" /><span id="message"></span>
<asp:LinkButton ID="LinkButton1" runat="server">取消</asp:LinkButton>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<span>当前时间:</span><asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click">
</asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
Button1_Click事件代码:
protected void Button1_Click(object sender, EventArgs e)
{
//刻意暂停6S
System.Threading.Thread.Sleep(6000);
Label1.Text = DateTime.Now.ToString();
}
转载于:https://www.cnblogs.com/dushouke/archive/2008/06/03/1212757.html
总结
以上是如意编程网为你收集整理的取消一个正在执行的异步回发的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: [转]一份ASP内存的释放的实验报告
- 下一篇: Enterprise Library 4