Cancel Asynchronous PostBack in C#


V4 Ajax Tutorials
Server Intellect Cloud Hosting

Cancel Asynchronous PostBack in C#

In this tutorial, you will learn how to cancel an Asynchronous PostBack that is currently in progress. This is useful when a user has submitted a request to a web page that uses AJAX, and they can see that their request is pending - through the use of an UpdateProgress, for example - and they decide they want to cancel that request for whatever reason. We can make this a reality for users with a little JavaScript.

For this example, we will create one main UpdatePanel and then a nested UpdatePanel and an UpdateProgress. We should have something like this:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UP1" runat="server">
<ContentTemplate>
<asp:UpdatePanel ID="UP2" runat="server">
<ContentTemplate>

</ContentTemplate>
</asp:UpdatePanel>

<asp:UpdateProgress ID="UPr1" runat="server" AssociatedUpdatePanelID="UP2">
<ProgressTemplate>

</ProgressTemplate>
</asp:UpdateProgress>
</ContentTemplate>
</asp:UpdatePanel>
</form>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Now we will place a Literal in the second UpdatePanel, and also a Button:

<asp:UpdatePanel ID="UP2" runat="server">
<ContentTemplate>
<asp:Literal ID="lit_DateTime" runat="server" />
<br />
<asp:Button ID="btn_GetTime" runat="server" Text="Get Current Time" OnClick="btn_GetTime_Click" />
</ContentTemplate>
</asp:UpdatePanel>

Notice here that we add a handler to the Button Click event. We will also set the Date and Time on Page_Load, but only the first time it loads - not on PostBacks. The code-behind will look something like this:

using System;

using System.Threading;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lit_DateTime.Text = DateTime.Now.ToString();
}
}

protected void btn_GetTime_Click(object sender, EventArgs e)
{
Thread.Sleep(3000);
lit_DateTime.Text = DateTime.Now.ToString();
}
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

On load and on button click we are simply displaying the current date and time in the Literal control. Also on the button click, we are putting it to sleep first, for 3 seconds, which will give us a chance to cancel the process before it's complete.

<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(startRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);

function startRequest(sender, e) {
//disable button during the AJAX call
document.getElementById('<%=btn_GetTime.ClientID%>').disabled = true;
document.getElementById('<%=btn_GetTime.ClientID%>').value = 'Getting time..';
}
function endRequest(sender, e) {
//re-enable button once the AJAX call has completed
document.getElementById('<%=btn_GetTime.ClientID%>').disabled = false;
document.getElementById('<%=btn_GetTime.ClientID%>').value = 'Get Current Time';
}
</script>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

Now all that is left to do is to add the JavaScript to handle the cancellation of the Async PostBack. It is important to note that this method will not actually stop the Server from processing the request; it will only cause the Page Request Manager to ignore the response from the Server. With this in mind, if the Server is performing a lengthy process, it may be worth making a new Async call to explicitly halt that activity.

Add the following script to the bottom of the page. This will check to see if the Cancel button is the one making the call, and if so, will abort the pending PostBack.

<script type="text/javascript">
var instance = Sys.WebForms.PageRequestManager.getInstance();
instance.add_initializeRequest(instance_initializeRequest);

function instance_initializeRequest(sender, args) {
if (args.get_postBackElement().id == 'btn_Cancel') {
instance.abortPostBack();
alert('Postback has been cancelled. Time will not be updated.');
}
}
</script>

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
kumar said:

thanks Server Intellect... great hosting.... http://www.planetsourcecode.in

Posted May 29, 2009 11:45 AM
crazy gifts shop said:

Effective analysis of Asynchronous PostBack that so verified. Please don't stop this method. You can suggest me by my link of crazy gifts shop.

Posted Dec 19, 2010 12:51 PM
Special gifts said:

I have been can to know by article because here has very effective data. It inspired me and its data can help me. I highly recommend it. It is very effective tutorials & likable.

Posted Dec 22, 2010 10:18 AM
Freelance SEO India said:

India’s leading Freelance SEO India services provider with main competency in Search Engine Optimization of websites.

Posted Dec 29, 2010 11:21 AM
levitra 10 mg said:

reality good code.

Posted Jan 31, 2011 3:27 PM
viagra for kvinner said:

It is hard to find knowledgeable people on this topic, but you sound like you know what you are talking about!

Posted Feb 7, 2011 6:08 AM
Austin Stucco Company said:

India’s leading Freelance SEO India services provider with main competency in Search Engine Optimization of websites.

Posted Feb 11, 2011 3:47 PM
Bamboo sheets said:

Thanks for posting this article. I am definitely tired of struggling to find relevant and intelligent commentary on this subject. Everyone nowadays seem to go to extremes to either drive home their viewpoint or suggest that everybody else in the globe is wrong. Thanks for your concise and relevant insight. Some additional thoughts that I finally got around to posting- <a href="http://www.buybamboosheets.com">Bamboo sheets</a>

Posted Mar 2, 2011 3:01 AM
Bamboo sheets said:

Thanks for posting this article. I am definitely tired of struggling to find relevant and intelligent commentary on this subject. Everyone nowadays seem to go to extremes to either drive home their viewpoint or suggest that everybody else in the globe is wrong. Thanks for your concise and relevant insight. Some additional thoughts that I finally got around to posting. http://www.buybamboosheets.com

Posted Mar 2, 2011 3:02 AM
maimi said:

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.<a href="http://chapter7lawoffices.com/">bankruptcy lawyer</a>

<a href="http://www.hfiainsurance.com/">auto insurance quotes</a>

Posted Mar 20, 2011 3:08 AM
moviefan1 said:

this is good website it is very useful to all kind of peoples and it has excellent content very much interested and very enjoy it thanks

for more info pls visit this links: http://moviesonlinefree.tv/

Posted Apr 24, 2011 10:43 PM
PDF Tools Review said:

Hi Great post and i'am trying to understand everything. Working with MVC was fine until the datagrid problem came up. My question is: I have to connect to a third party system which I can query with a specific language. The result is given back as a datatable with all the rows and columns. Can I connect/use this datatable as a datasource for the grid? And if so, please advise how to do this.

Posted May 14, 2011 7:09 AM
Dental Practice Preston said:

Great verification of the analysis

Posted May 24, 2011 5:35 AM
LCD Or Plasma said:

The program you have provided is very helpful. I have got a clear idea about it. Thanks for sharing your idea.@

Posted May 25, 2011 4:53 PM
Cedar Park Roofing Companies said:

Thanks you for this post.Actually i have been searching this types of post .This is really informative.

Posted Jun 26, 2011 2:30 PM
male ed pills said:

where I find lot of useful tutorials. thanks

Posted Jul 7, 2011 4:46 AM
cartier bracelet love said:

It's so great that you guys are sticking with it! I've been following the team for some time and am so happy that

now the fire is burning brighter than ever! Way to stay positive and stick with something you love!

http://www.ctlove2u.org

Posted Jul 13, 2011 6:33 AM
hosting said:

thanks for the code....

Posted Jul 20, 2011 1:02 PM
web based medical billing said:

Very nice and helpful information has been given in this article. I must say that this is a great post. I loved reading it..

Posted Jul 23, 2011 8:45 AM
radio stations said:

Superb. It gives more idea to people about streams . U have to improve ur web to understand easily. Thanks.

Posted Jul 23, 2011 8:45 AM
Hermes said:

The i cars also signal

Posted Jul 31, 2011 9:04 PM
www.outlet-louisvuitton.co.uk said:

www.outlet-louisvuitton.co.uk

Posted Aug 6, 2011 3:29 AM
healthcare it said:

I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading.

Posted Aug 23, 2011 9:36 AM
lacoste uk said:

I think the post is good for us.

Posted Sep 26, 2011 4:40 AM
Tim Burton said:

Of course, what a magnificent site and revealing posts, I surely will bookmark your site.Have an awesome day!

Posted Sep 27, 2011 9:30 PM
Rejser til Thailand said:

This is a spectacular article that I have really enjoyed. Hopefully I can learn more from your valuable experience. Thanks a lot for sharing this and please keep on posting more.

Posted Oct 1, 2011 7:09 AM
Asics Running said:

http://www.asicsaustore.com/ Asics Running

Asics Shoes

Asics Tiger

Onitsuka Tiger Sale

Posted Oct 21, 2011 9:27 PM
John Howard said:

Great work.............!

Posted Nov 10, 2011 2:45 PM
boiler installation london said:

So helpful code for me.

Posted Nov 14, 2011 6:17 AM
Adidas Jeremy Scott said:

My good friends recommend this site to me.I have to say that the content

here was the most completed that I found anywhere.I will be back to

check some more information on this.Thanks

Posted Nov 19, 2011 3:49 AM
Adidas Jeremy Scott said:

My good friends recommend this site to me.I have to say that the content

here was the most completed that I found anywhere.I will be back to

check some more information on this.Thanks

Posted Nov 19, 2011 3:50 AM

Leave a Comment