AJAX Timer control in ASP.NET using VB.NET

In this article you will learn that how to use AJAX Timer control to update a region of a page.
  • 6197
Download Files:
 

Introduction

The Timer control is pretty simple to use at a specified interval, The control fires its tick event. Inside an Event handler for this Event you can execute any code see fit if you hook up the Timer to the UpdatePanel using its Triggers Collection, you can update a single region of a page whenever the Timer control Ticks-that is when it fires its Tick Event. Here we are Discussing an example using the Timer control in an aspx page to update the region of a page. The Timer will tick every one second and update a Label. The implementation of example needs a ScriptManager, Timer and a Label control.
 
Getting Started

  • Simply create a new ASP.NET web application.
  • Drag a ScriptManager, Timer and a Label control on your web page. The page will look like Below.

    ajaxtimer1.gif
     
  • You can also add control by the below code.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html
    xmlns="http://www.w3.org/1999/xhtml">
    <
    head id="Head1" runat="server">
        <title>How to Use AJAX Timer</title>
    </
    head>
    <
    body>
        <form id="form1" runat="server">
        <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        </div>
          <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <br />
                  <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="XX-Large"
                       
    ForeColor="#0033CC"></asp:Label>
                </ContentTemplate>
                <Triggers>
                  <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                </Triggers>
           </asp:UpdatePanel>
              <br />
              <asp:Timer ID="Timer1" runat="server" Interval="60" ontick="Timer1_Tick"></asp:Timer>
        </form>
    </
    body>
    </
    html>
     
  • Then attach the below code in code behind file.

       
    Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            UpdateLabel()
       
    End Sub
        Private Sub UpdateLabel()
            Label1.Text =
    DateTime.Now.ToString()
       
    End Sub
     
  • Now run your application.

Output

ajaxtimer2..gif

Summary

In this article you Learned that how to use AJAX Timer control.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.