Language Autoeventwireup Codebehind Inherits HTML Public: Eventargs
Language Autoeventwireup Codebehind Inherits HTML Public: Eventargs
While it's perfectly possible to use Ajax without Microsoft ASP.NET AJAX, a lot of things are way
easier, since Microsoft has wrapped some of most tedious parts of Ajax into their
implementation. For instance, the 3 most popular browsers requires different ways of using Ajax,
and have different JavaScript implementations. ASP.NET AJAX simplifies this a lot and allows you
to write the same code to target all 3 major browsers.
</html>
Try running the example site, and click the button. The label will be updated with our usual Hello
world text, and the current time. Try repeatedly clicking the button, and you will see the label
get the current timestamp each time. Notice the wonderful absence of a blinking window and a
running status bar - everything is done without updating anything but the label! We've just
created our first AJAX enabled page. If you wish to see how this page would work without AJAX,
try setting the "enablepartialrendering" of the ScriptManager to false like this:
<asp:ScriptManager
ID="MainScriptManager" runat="server"
enablepartialrendering="false" />
The UpdatePanel control is probably the most important control in the ASP.NET AJAX package. It
will AJAX'ify controls contained within it, allowing partial rendering of the area. We already used
it in the Hello world example, and in this chapter, we will go in depth with more aspects of the
control.
The <asp:UpdatePanel> tag has two childtags - the ContentTemplate and the Triggers tags.
The ContentTemplate tag is required, since it holds the content of the panel. The content can be
anything that you would normally put on your page, from literal text to web controls. The
Triggers tag allows you to define certain triggers which will make the panel update it's content.
The following example will show the use of both childtags.
</html>
So, what's this example all about? Try running it, and click the two buttons. You will notice that
then first button updates only the first datestamp, while the second button updates both. Why?
We have set the Panels to update conditionally, which means that their content is only updated if
something insides them causes a postback, or if one of the defined triggers are fired. As you can
see, the first UpdatePanel carries a trigger which references the second button. This will ensure
that the first panel is updated even when a control on a different UpdatePanel is used. The
AsyncPostBackTrigger tag is pretty simple - it only takes two attributes, the controlid, a
reference to the control which can trigger it, and the eventname, which tells which eventtype can
cause the trigger to fire. If you wish for the content of a UpdatePanel to be updated no matter
what, you may change the updatemode property to Always.
In general, you should only have UpdatePanels around areas where you wish to do partial
updates. Don't wrap your entire page within an UpdatePanel, and don't be afraid to use several
panels, since this will give you more control of which areas update and when they do it.
One of the problems with Ajax, is the fact that since it's asynchronus and in the background, the
browser will not show you any status. With fast servers and fast methods, this is not a big
problem, but whenever you have a method which takes up a bit of time, the user is very likely to
get impatient. Fortunately, ASP.NET AJAX solves this problem for us as well, with a nice control
called UpdateProgress. It will use your own template to show that an asynchronus method is
working. Have a look at the following example, which will show the control in action. It will be
explained afterwards.
</html>
This simple example will just show you how easy it is to use the UpdateProgress control. Once
the button is clicked, the script sleeps for 5 seconds (don't use code like that in your real
projects - it's for demonstrational purposes only!), and the "Loading..." text is displayed on your
page. You can use anything in the ProgressTemplate, including ordinary markup and other
controls. A common use is an animated GIF, positioned strategically on the page using CSS
positioning.
You can have multiple UpdateProgress controls on the page, and by using the
AssociatedUpdatePanelID property, you can make sure that the UpdateProgress is only shown
when a certain UpdatePanel is updated.
The DynamicLayout property is nice to know as well. It tells whether or not the page should
reserve space for your progress control. If it's set to true, which is the default, the space is
dynamic, hence it's not reserved, but taken when the control is shown. If you wish to reserve the
space, set this property to false. To see the difference, add the property to our example and
change it back and forth.
If some of your postbacks are fast, the UpdateProgress will only be shown for a very short
amount of time, resulting in a blinking behavior, which may confuse your users. For that reason,
you may specify a minimum amount of time to occur before showing the progress control. This
can be done with the DisplayAfter attribute. Specify a number of milliseconds to elapse before
showing the progress control, e.g. 2000 if you wish to wait for 2 seconds.
Timer controls allow you to do postbacks at certain intervals. If used together with UpdatePanels,
which is the most common approach, it allows for timed partial updates of your page, but it can
be used for posting back the entire page as well. In this chapter we will focus on using timers
with UpdatePanels, so if you haven't already read the chapter on UpdatePanels, please do so
now.
Here is a small example of using the Timer control. It simply updates a timestamp every 5
seconds.
</html>
This is all very simple. We have a normal UpdatePanel, which carries a Trigger reference to our
new Timer control. This means that the panel is updated when the Timer "ticks", that is, fires the
Tick event. The Timer control uses the interval attribute to define the number of milliseconds to
occur before firing the Tick event. As you can see from our CodeBehind code listing, we just
update the DateStampLabel each time the Timer fires. This could be done more efficient with a
simple piece of JavaScript, which updates the time on the clientside instead of involving the
server. The example is only used to demonstrate the potential of the Timer control.
Another approach is including the Timer inside the UpdatePanel. Doing so would save us from
defining a trigger, but you should be aware of the fact that the behavior will be different,
depending on whether you have the Timer inside or outside an UpdatePanel. When a Timer is
inside an UpdatePanel, the Timer is not re-constructed until the UpdatePanel is fully updated.
That means that if you have a Timer with an interval of 60 seconds, and the update takes 5
seconds, the next event won't be fired 60 seconds after the previous, but 65 seconds after. On
the other hand, if the Timer is outside the UpdatePanel, the user will only look at the content of
the panel for 55 seconds before it's updated again.
You should always remember that even though partial updates are not as heavy on the server as
real postbacks, the server is still contacted, and when using timers, you may get a lot of partial
postbacks, which can slow things down. Always use as high intervals as possible, and consider if
contacting the server is really necessary or not.