AJAX Asynchronous PostBack Precedence in C#


V4 Ajax Tutorials
Server Intellect Cloud Hosting

AJAX Asynchronous PostBack Precedence in C#

AJAX brings a lot of functionality to Web Applications, and an almost desktop-like experience. Users no longer have to wait for the whole page to be resent from the Web Server when making a small request on the page. AJAX has the ability to update areas of a page separately using UpdatePanels. However, when there are multiple UpdatePanels on a page, the user may mistakingly begin another Asynchronous Request while one is being processed. For example, if a user makes one change on the web page and the process is still active when they make another AJAX request, the second request will take precedence over the first. This means the first request will never complete.

In this tutorial, you will learn how to use AJAX to detect if an Asynchronous request is still being processed when another is called. By doing this, we can notify the user, and allow the first request to complete before starting another.
To demonstrate this, we will create two UpdatePanels, each displaying the current date and time, and with a button. Each button will update its own UpdatePanel when clicked. However, when the first button is clicked, we will purposely make the process take 3 seconds to complete, giving us enough time to click the second button to test. Upon clicking the second button while the first is still processing, we should be notified that a process is still active and not allow the second process to initiate.

Let's start by adding the ScriptManager and the two UpdatePanels:

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

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

</ContentTemplate>
</asp:UpdatePanel>

<br /><br />
<asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
<ContentTemplate>

</ContentTemplate>
</asp:UpdatePanel>
<br /><br />
</form>

Notice we set the UpdateMode on both Panels to Conditional. This is because we do not want them both updating at the same time - each button should update their own UpdatePanel only.
Let's go ahead and add the buttons and the current time to the UpdatePanels:

<asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= DateTime.Now.ToString() %>
<br />
<asp:Button ID="btn_GetTime1" runat="server" Text="Get Time (1)" OnClick="btn_GetTime1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<br /><br /> <asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= DateTime.Now.ToString() %>
<br />
<asp:Button ID="btn_GetTime2" runat="server" Text="Get Time (2)" />
</ContentTemplate>
</asp:UpdatePanel>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Notice we include a handler for the first Button - here we will make the thread sleep for 3 seconds. This will be done in the code-behind like so:

using System.Threading;

..

protected void btn_GetTime1_Click(object sender, EventArgs e)
{
Thread.Sleep(3000);
}

To make the request more visual, let's add an UpdateProgress into the mix as well. This will show us when a request is in progress:

<asp:UpdateProgress ID="UPr1" runat="server" AssociatedUpdatePanelID="UP1">
<ProgressTemplate>
<br /><br />
Please wait..
</ProgressTemplate>
</asp:UpdateProgress>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

We will also include some JavaScript to check if an Async PostBack is currently in progress. To do this, add the following script:

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

function instance_initializeRequest(sender, args) {
if (instance.get_isInAsyncPostBack()) {
alert('Still processing request. Please wait..');
args.set_cancel(true);
}
}
</script>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Now when we push the first button, it will wait for 3 seconds before updating the time. If you push the second button before the time updates, we will get a popup message notifying us that the request is still processing.
The ASPX page will look something like this:

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

<asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= DateTime.Now.ToString() %>
<br />
<asp:Button ID="btn_GetTime1" runat="server" Text="Get Time (1)" OnClick="btn_GetTime1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UPr1" runat="server" AssociatedUpdatePanelID="UP1">
<ProgressTemplate>
<br /><br />
Please wait..
</ProgressTemplate>
</asp:UpdateProgress>
<br /><br />
<asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= DateTime.Now.ToString() %>
<br />
<asp:Button ID="btn_GetTime2" runat="server" Text="Get Time (2)" />
</ContentTemplate>
</asp:UpdatePanel>
<br /><br />
</form>
</body>
</html>
<script type="text/javascript">
var instance = Sys.WebForms.PageRequestManager.getInstance();
instance.add_initializeRequest(instance_initializeRequest);

function instance_initializeRequest(sender, args) {
if (instance.get_isInAsyncPostBack()) {
alert('Still processing request. Please wait..');
args.set_cancel(true);
}
}
</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
Great Work said:

great work and thanks

http://www.planetsourcecode.in

Posted May 7, 2009 12:37 PM
Ken Parker said:

This works Great! This should eliminate a lot of frustration amoung our impatient endusers executing multiple server requests at the same time and creating a lot of havoc!

Thanks!!

Posted Jul 14, 2009 1:21 PM
AJAX and Javascript forever :-) said:

Like every new technology it adds a lot of developer pain in exchange for something that the end-user takes for granted.

Posted Aug 23, 2009 9:31 AM
gerry said:

Love this tutorial. I will show this to my colleagues at our <a href="http://www.shedplansbuilding.com/review-my-shed-plans.asp">Review Of My Shed Plans</a> office for their reference.

Posted Dec 17, 2010 4:17 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
herbal ed pills said:

Really good ideas.

Posted Jan 31, 2011 3:29 PM
psoriasis said:

This really is such a excellent post to read. Stimulating me to read much more of your posts. Continue the good work. Planning to see a lot more excellent articles from you soon.

Posted Feb 7, 2011 7:19 AM
white spots on skin said:

And I liked it, I’ll look at your site.

Thank you for what you give is for those who want to learn

Posted Feb 12, 2011 12:56 AM
zithromax sans ordonnance said:

Thanks for taking time to write this post. Sounds good, curious to check it out.

Posted Mar 9, 2011 5:18 PM
Anurag said:

Thanks for posting such info... :)

Posted Apr 18, 2011 4:30 AM
Printing Miami said:

12th Avenue Graphics has become a leader in digital pre-press and printing with the latest and most effecient equipment available in South Florida. Generating top quality printing that is both on time and price competitive. Save on Commercial Printing! Online Printing Services for Business Cards, Postcards, Brochures, Posters and Canvas Prints! Follow us by <a href="http://www.12avenuegraphics.com">Printing Miami</a>, <a href="http://westpalmbeachprinting.net">printing west palm beach</a> , <a href="http://printingftlauderdale.com">Printing ft lauderdale</a> and <a href="http://www.alaskaautoshipping.net">Auto shipping alaska</a> links to know us.

Posted May 19, 2011 2:51 AM
hosting said:

It is really helpful article about the topic of . I so much interest about your article. I know it is not any simple information. This is very nice post! I will bookmark this blog as I have <a href="http://www.sitiohost.cl">hosting</a> , <a href="http://www.12avenuegraphics.com">Printing Miami</a> and <a href="http://www.fighterwarehouse.com">MMA Shorts</a>

Posted May 23, 2011 2:08 AM
kia warranty cost said:

This is another great article with very useful tips about. I am hoping to pick up more blogging work, too! I like to review my <a href="http://kiaextendedwarranties.com/">kia factory warranty</a> , <a href="http://kiaextendedwarranties.com/">kia extended warranty</a>, <a href="http://kiaextendedwarranties.com/">kia warranty</a> and <a href="http://kiaextendedwarranties.com/">kia warranty cost</a>

Posted Jun 7, 2011 10:56 PM
premature ejaculation medicine said:

very good. thanks

Posted Jul 15, 2011 5:24 AM
storage unit Sacramento said:

It’s always good to learn tips like your posting. I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too. Thanks and keep posting such an informative blogs. You may know me by <a href="http://longviewselfstorage.com/">storage unit Sacramento</a> , <a href="http://longviewselfstorage.com/">self storage Sacramento</a> and <a href="http://longviewselfstorage.com/">storage facility Sacramento</a>

Posted Jul 23, 2011 1:32 AM
plavix usa said:

Sounds good, curious to check it out

Posted Jul 28, 2011 4:06 AM
ed trial packs said:

Thanks so much! I will keep checking your posts.

Posted Jul 28, 2011 4:12 AM
www.outlet-louisvuitton.us said:

www.outlet-louisvuitton.us

Posted Aug 6, 2011 3:36 AM
storage unit Sacramento said:

It is a great post on AJAX Asynchronous PostBack Precedence in C# and a great idea. Always I wanted to jazz up the icons on my websites a little. It has some important idea. This is very nice post! I will bookmark this blog. <a href="http://longviewselfstorage.com/">storage unit Sacramento</a> , <a href="http://longviewselfstorage.com/">self storage Sacramento</a> and <a href="http://longviewselfstorage.com/">storage facility Sacramento</a>

Posted Aug 27, 2011 9:40 AM
black cocktail dress said:

The code below can go in the <head> tags of the ASPX page:

<script language="javascript" type="text/javascript">

function GetData()

{

var response;

Ajax_CSharp.GetData(GetData_CallBack);

}

function GetData_CallBack(response)

{

var response=response.value;

if(response=="Empty")

{

alert("No Record Found !!!");

}

else if(response=='Error')

{

alert("An Error occured in accessing the DataBase !!!");

}

else

{

var arr=response.split("~");

var empID=arr[0].split(",");

var empName=arr[1].split(",");

document.getElementById('dlistEmployee').length=0;

for(var i=0;i<empID.length;i++)

{

var o = document.createElement("option");

o.value = empID[i];

o.text = empName[i];

document.getElementById('dlistEmployee').add(o);

}

}

}

function display()

{

var selIndex=document.getElementById("dlistEmployee").selectedIndex;

var empName=document.getElementById("dlistEmployee").options(selIndex).text;

var empID=document.getElementById("dlistEmployee").options(selIndex).value;

document.getElementById("lblResult").innerHTML="You have selected " + empName + " ( ID : " + empID + " )";

}

</script>

Posted Sep 3, 2011 4:44 PM
helen olsson said:

I just came across your blog and reading your beautiful words. I thought I would leave my first comment but I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Helen Olsson

Posted Oct 30, 2011 5:07 AM

Leave a Comment