Skip to main content

Cancelling an Asynchronous Request




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxCancel.aspx.cs"
Inherits="AjaxCancel" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="S1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= System.DateTime.Now.ToLongTimeString() %>
<asp:Button ID="btnRefresh" runat="server" Text="Update"
OnClick="btnRefresh_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>

<script type="text/javascript">
Sys.Application.add_init(Init);
var prm = null;

function Init(sender)
{
prm = Sys.WebForms.PageRequestManager.getInstance();
//Ensure EnablePartialRendering isn't false which will prevent
//accessing an instance of the PageRequestManager
if (prm)
{
if (!prm.get_isInAsyncPostBack())
{
prm.add_initializeRequest(InitRequest);
}
}
}
function InitRequest(sender,args)
{

if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id == 'btnRefresh') {
//Could abort current request by using: prm.abortPostBack();
//Cancel most recent request so that previous request will complete
//and display
args.set_cancel(true);
alert("A request is currently being processed. Please " +
"wait before refreshing again.");
}
}
</script>

</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class AjaxCancel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}
protected void btnRefresh_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
}
}

Comments

Popular posts from this blog