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.
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
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.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
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.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
} |