Tutorial: Introduction to AJAX Timer Control in ASP.NET and VB



Tutorial: Introduction to AJAX Timer Control in ASP.NET and VB

This tutorial was created with Microsoft's ASP.NET AJAX Extensions, which can be downloaded at this link

In this tutorial, we will introduce you to the AJAX Timer Control. This Control can be used to initiate a refresh of an UpdatePanel in a certain timeframe. We are also able to refresh the entire page using the Timer Control. This simple examples illustrates how the Timer Control works and how to implement it.

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!

First, we start by creating an AJAX-Enabled web site in Visual Studio .NET 2005, with the AJAX Extensions installed.
The AJAX Extensions (from Microsoft) make it a whole lot easier to create AJAX web pages, as Visual Studio will add in the necessary assembly references, etc. into the Web.config

When we first open our Default.aspx page, we should already have a Script Manager:

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

Next, we will create a Timer and an UpdatePanel:

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

<asp:Timer runat="server" id="Timer1" interval="5000" ontick="Timer1_Tick" />
<asp:UpdatePanel runat="server" id="TimePanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="Timer1" eventname="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label runat="server" id="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
</form>

Note the ontick and interval attributes of the Timer Control. The interval is in milliseconds, so every 5 seconds, the UpdateTimer_Tick method will be called.
Also note the eventname attribute of the Trigger. This tells the page what is going to cause (or trigger) the UpdatePanel to update.

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.

Finally, we add logic to the code-behind. We are simply going to tell it to display the current time in a Label. This way we can see exactly when the UpdatePanel is being refreshed.

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = DateTime.Now.ToString()
End Sub
End Class



Comments

There are no comments yet...Kick things off by filling out the form below.


Leave a Comment