<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blog.csp.uwa.edu.au/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Adam's Blog : introduction</title><link>http://blog.csp.uwa.edu.au/adam/archive/tags/introduction/default.aspx</link><description>Tags: introduction</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.2)</generator><item><title>AJAX in ASP.NET 2.0 - Introduction To The Basics</title><link>http://blog.csp.uwa.edu.au/adam/archive/2007/04/17/ajax-in-asp-net-2-0-introduction-to-the-basics.aspx</link><pubDate>Tue, 17 Apr 2007 02:59:00 GMT</pubDate><guid isPermaLink="false">06c7d2c2-7ef8-4788-b25e-93d668b21aa2:28</guid><dc:creator>adam.n</dc:creator><slash:comments>0</slash:comments><comments>http://blog.csp.uwa.edu.au/adam/comments/28.aspx</comments><wfw:commentRss>http://blog.csp.uwa.edu.au/adam/commentrss.aspx?PostID=28</wfw:commentRss><description>&lt;p&gt;&lt;i&gt;This is a copy of the &lt;a href="http://noizwaves.bur.st/2007-04-16/ajax-in-aspnet-20-introduction-to-the-basics.html" title="Original post on Noiz Waves" target="_blank"&gt;post&lt;/a&gt; on my blog &lt;a href="http://noizwaves.bur.st/" title="Noiz Waves, blog of Adam Neumann"&gt;Noiz Waves&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/AJAX" title="Wikipedia AJAX" target="_blank"&gt;AJAX&lt;/a&gt;
has become quite the buzzword lately when it comes to Web 2.0. Luckily,
AJAX is really easy to integrate into new and existing ASP.NET
application; it's just a matter of adding some simple controls here and
there. The best thing about AJAX in ASP.NET is that no complex coding
is required; all the postbacks and &lt;a href="http://en.wikipedia.org/wiki/Javascript" title="Wikipedia Javascript" target="_blank"&gt;Javascript&lt;/a&gt; is abstracted away.&lt;/p&gt;
&lt;p&gt;In this article I want to provide a quick introduction to AJAX in
ASP.NET, highlighting core ideas and useful hints, aimed for existing
developers who want to dabble with AJAX. I'm going to start with
installing &lt;a href="http://ajax.asp.net/" title="ASP.NET AJAX" target="_blank"&gt;ASP.NET AJAX&lt;/a&gt;
and configuring Visual Studio 2005 (which takes no time at all). Then
its onto a description of 3 of the main controls that utilise AJAX; the
ScriptManager, UpdatePanel and Timer. Finally, I will create a small
project that combines these controls to perform a simple task.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Installing ASP.NET AJAX&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Firstly, you need to have Visual Studio 2005 and .NET 2.0 installed on your machine. Once you have done this, you need to &lt;a href="http://ajax.asp.net/downloads/default.aspx?tabid=47" title="Download ASP.NET AJAX Extensions" target="_blank"&gt;download the ASP.NET AJAX Extensions 1.0&lt;/a&gt;
(version correct at time of writing). The download is only a couple of
megabytes and provides the essentials for AJAX'ing your websites.
Whilst here, you can also download the &lt;a href="http://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=AtlasControlToolkit" title="ASP.NET AJAX Control Toolkit" target="_blank"&gt;ASP.NET AJAX Control Toolkit&lt;/a&gt;. It provides a large number of AJAX'ed controls which are ready to use.&lt;/p&gt;
&lt;p&gt;Once the extensions have been installed, all that needs doing is
adding the AJAX extensions to the Visual Studio toolbox. I added the
controls to a new tab called 'AJAX Extensions' by right-clicking on the
tab selecting Choose Items and browsing to "C:\Program Files\Microsoft
ASP.NET\ASP.NET 2.0 AJAX
Extensions\v1.0.61025\System.Web.Extensions.dll" then clicking OK. If
all goes well, your toolbox will look like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://noizwaves.bur.st/wp-content/uploads/2007/04/ajax1.png" alt="ASP.NET AJAX 1"&gt;&lt;/p&gt;
&lt;p&gt;That's it! AJAX has been installed, and its ready to be used. If you
want the AJAX Control Toolkit controls in your toolbox, the procedure
is similar, but you browse to "C:\Program Files\Microsoft ASP.NET\Ajax
Control Toolkit\AjaxControlToolkit.dll". Now, onto the controls...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The ScriptManager&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://noizwaves.bur.st/wp-content/uploads/2007/04/ajax2.png" alt="AJAX in ASP.NET 2.0 - Introduction To The Basics"&gt;&lt;/p&gt;
&lt;p&gt;In a nutshell, the ScriptManager is the control that handles most of
the behind the scenes Javascript side of AJAX. There must be a
ScriptManager on any page that uses AJAX. The easiest way to achieve
this is to put one at the top of the &lt;a href="http://msdn2.microsoft.com/en-us/library/wtxbf3hh.aspx" title="Introduction to Master Pages" target="_blank"&gt;master page&lt;/a&gt;.
As far as properties go, EnablePartialRendering is the single most
important one to be familiar with. In a nutshell, it enables or
disables asynchronous postbacks. Disabling partial rendering forces all
postbacks to be full postbacks. This is extremely handy when debugging
some errors, as the error provided with partial rendering is a small
javascript popup.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The UpdatePanel&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://noizwaves.bur.st/wp-content/uploads/2007/04/ajax3.png" alt="ASP.NET AJAX 3"&gt;&lt;/p&gt;
&lt;p&gt;The UpdatePanel is the easiest way to use AJAX to improve the
responsiveness of a web application using AJAX. When an event occurs
that is deemed to be a trigger for the panel, the server processes the
request and responds with the updated contents of the UpdatePanel
INSTEAD of the contents of the entire page. What this means is that
user interactions affecting a small area of the page will only cause
that small area to refresh. This results in a sizable saving of
bandwidth, and a noticeable increase in response time.&lt;/p&gt;
&lt;p&gt;UpdatePanels have a few important parameters that can require
tweaking. ChildrenAsTriggers causes the panel to be refreshed when ever
a child control fires an event. In some situations that is undesirable,
as unnecessary asynchronous postbacks will occur. Triggers is a
collection of control + events pairs on the page that will cause the
update panel to refresh. This is my preferred way to specify when the
contents of the panel are refreshed. UpdateMode specifies whether the
update panel will refresh its contents on any postback or only those
that trigger the panel. Again, I set this to 'Conditional' as it gives
me finer control over when the panel is refreshed.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The Timer&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://noizwaves.bur.st/wp-content/uploads/2007/04/ajax4.png" alt="ASP.NET AJAX 4"&gt;&lt;/p&gt;
&lt;p&gt;This control is one that i had no use for during the first few
months of developing, but after tinkering with the Timer it is
extremely useful in some situations. A Timer fires a Tick event at
regular intervals, and these ticks can be used as triggers for
UpdatePanels. In other words, asynchronous postbacks can occur without
any interaction from the user at all. This would come in handy for
regularly saving the contents of a textbox or loading live stock prices
for example. Timer has a couple of simple but useful properties, and
these are Interval (the delay in milliseconds between tick events being
fired) and Enabled (should the timer fire tick events or not).&lt;/p&gt;
&lt;p&gt;It is possible to get tricky and put a Timer inside an UpdatePanel,
and change the Timer's properties at run time. By setting the Interval
to a very small number, and then immediately disabling the timer in the
tick event handler, only one tick event will occur. This is an
effective way of running a section of code only when the page has
finished loading in the client's browser.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Sample ASP.NET AJAX Website Project&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The sample project I've included with this article uses all three of
the described AJAX controls. It contains a ScriptManager at the top, an
UpdatePanel which contains some labels whose contents will be changed
and refreshed asynchonously, and a Timer to trigger the change in label
text and cause the actual asynchronous postback.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://noizwaves.bur.st/wp-content/uploads/2007/04/ajax5.png" alt="ASP.NET AJAX 5"&gt;&lt;/p&gt;
&lt;p&gt;There is only 2 line of actual C# code in this simple project, and
they set the label text to a random integer. Other than that, no
additional coding was required to implement an AJAX'ed website. Pretty
good eh'?&lt;/p&gt;
&lt;p&gt;From the use of 3 basic controls, anyone can AJAX up their projects,
making them both better to use and more efficient to serve. There are a
couple of other controls included in the ASP.NET AJAX Extensions that I
haven't touched on here; they are the ScriptManagerProxy and
UpdatProgress. I would strongly suggest that everyone muck around with
the UpdateProgress control. From a usability point of view, it is
essential to know when the page is performing behind the scenes, not
just for the users of the site, but also the developers and debuggers.&lt;/p&gt;
&lt;p&gt;Attachments: &lt;a href="http://noizwaves.bur.st/wp-content/uploads/2007/04/sampleajaxwebsite.zip" title="SampleAJAXWebsite.zip"&gt;SampleAJAXWebsite.zip&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://blog.csp.uwa.edu.au/aggbug.aspx?PostID=28" width="1" height="1"&gt;</description><category domain="http://blog.csp.uwa.edu.au/adam/archive/tags/asp.net/default.aspx">asp.net</category><category domain="http://blog.csp.uwa.edu.au/adam/archive/tags/sample/default.aspx">sample</category><category domain="http://blog.csp.uwa.edu.au/adam/archive/tags/basics/default.aspx">basics</category><category domain="http://blog.csp.uwa.edu.au/adam/archive/tags/updatepanel/default.aspx">updatepanel</category><category domain="http://blog.csp.uwa.edu.au/adam/archive/tags/scriptmanager/default.aspx">scriptmanager</category><category domain="http://blog.csp.uwa.edu.au/adam/archive/tags/ajax/default.aspx">ajax</category><category domain="http://blog.csp.uwa.edu.au/adam/archive/tags/introduction/default.aspx">introduction</category><category domain="http://blog.csp.uwa.edu.au/adam/archive/tags/timer/default.aspx">timer</category></item></channel></rss>