<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Atlantic BT &#187; real time updates</title>
	<atom:link href="http://www.atlanticbt.com/blog/tag/real-time-updates/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.atlanticbt.com/blog</link>
	<description>Internet Marketing and Web Development in Raleigh</description>
	<lastBuildDate>Wed, 08 Feb 2012 02:40:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Updating a .NET Page in Real-Time Using jQuery and AJAX</title>
		<link>http://www.atlanticbt.com/blog/updating-a-net-page-in-real-time-using-jquery-and-ajax/</link>
		<comments>http://www.atlanticbt.com/blog/updating-a-net-page-in-real-time-using-jquery-and-ajax/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 18:40:50 +0000</pubDate>
		<dc:creator>Jeremy Wiggins</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[postbacks]]></category>
		<category><![CDATA[real time]]></category>
		<category><![CDATA[real time updates]]></category>

		<guid isPermaLink="false">http://www.atlanticbt.com/blog/?p=1705</guid>
		<description><![CDATA[Here&#8217;s a problem I bet you&#8217;ve run into before. You have a long-running function on one of your .NET pages and you want to provide real-time feedback to the user while they&#8217;re waiting on the function to complete (by function, I mean a method or a set of methods in the code-behind). So you put [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a problem I bet you&#8217;ve run into before.  You have a long-running function on one of your .NET pages and you want to provide real-time feedback to the user while they&#8217;re waiting on the function to complete (by function, I mean a method or a set of methods in the code-behind).  So you put a variable somewhere on your page and you update it as the function runs.  Then you realize that the variable value won&#8217;t actually change on the front-end until the postback is complete, which in turn means that it will get updated only once and say &#8220;Finished.&#8221;  Just in time to tell the user what they already know.</p>
<p>To get around this conundrum, I present to you jquery and ajax, my two favorite web technologies ever.  <abbr title="Asynchronous JavaScript + XML">AJAX</abbr> is a group of technologies that allows you to interact with the server from the client-side without affecting the behavior of the existing page.  What this means is that you can post to a page without getting the associated postback from .NET (and subsequent page refresh).  If that doesn&#8217;t sound important, trust me, it is.  If we can post to a page without getting a postback, then we&#8217;re already pretty close to real-time updates.  All we need to do now is use jQuery to update the <acronym title="Document Object Model">DOM</acronym> as we do posts.  Sound complicated?  It isn&#8217;t.</p>
<p>Back to our example, suppose a user clicks a button and that button fires a postback and runs 3 methods in the code-behind, DoStep1(), DoStep2(), and DoStep3().  The first thing we need to do is simply move these methods to a new page.  If your original page was called <strong>DoWork.aspx</strong>, then a good name for the new page would be <strong>DoWork_Ajax.aspx</strong>.</p>
<p>Now, back on <strong>DoWork.aspx</strong>, you probably have an asp element on your page that you&#8217;d like to update as the functions progress.  You can replace this with a standard HTML element.  <code>DoWork()</code> is a javascript function that will update this element.  It looks like this:</p>
<pre>
function DoWork(step)
{
    if (step == 1)
    {
        $("#update-msg").html("Starting Work");
    }

    if (step == 4)
    {
        $("#update-msg").html("Finished");
        return false;
    }        

    $("#update-msg").load(
        "DoWork_Ajax.aspx #results span",
        { workStep: step },
        function(){ DoWork(step + 1); }
    );
}<span></span>
</pre>
<p>Assume that <code>#update-message</code> is a span on our page, i.e. a placeholder for the status of our worker functions.</p>
<p>The logic flows as such:  If we&#8217;re on step 1, tell the user we&#8217;re starting.  If we make it past step 3, we&#8217;re done, so tell the user that too and return from this function.  Finally, the meat of the function &#8211; Do an asyncronous post to <strong>DoWork_Ajax.aspx</strong>, telling it what step we&#8217;re on, and injecting a span from it into the DOM for the current page.</p>
<p>The <code>PageLoad</code> method for <strong>DoWork_Ajax.aspx</strong> looks like this:</p>
<pre>
switch (Request["workStep"])
{
    case "1":
        DoStep1();
        break;
    case "2":
        DoStep2();
        break;
    case "3":
        DoStep3();
        break;
}
</pre>
<p>Each method updates an element on the page, just like before.  This is the element that gets injected into the DOM by our jQuery function on <strong>DoWork.aspx.</strong></p>
<p>You&#8217;re doing the exact same work as before, but now you can update the page in real time.  I&#8217;ve included a <a href="/downloads/AjaxJQSampleApp.zip">sample application</a> that demonstrates everything I&#8217;ve talked about.  For those that are interested, you can download the project and run it locally to see this in action.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atlanticbt.com/blog/updating-a-net-page-in-real-time-using-jquery-and-ajax/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/13 queries in 0.005 seconds using disk: basic
Object Caching 262/285 objects using disk: basic
Content Delivery Network via d1rvlzmuzboe2s.cloudfront.net

Served from: www.atlanticbt.com @ 2012-02-08 15:40:53 -->
