Cooldharma06
Cooldharma06

Reputation: 505

How to set a count using timer in asp.net

I have an asp.net page in that I have a label in that there is a number(60) which has to decrease for each second. When the number reaches 0 it has to pop-out a message.

I searched and found some timer codes, tried to understand, but it is not clear.

I have an idea but don't know how to implement this.

"Initially the label is set to 60, for each interval of (1000) 60 is decreased by one. If the label value reaches 0, alert or message is shown, and label text is reset to 60".

If you have any other idea or way to implement this , do share..

Upvotes: 0

Views: 5400

Answers (2)

ChitranjanThakur
ChitranjanThakur

Reputation: 91

Tried n Tested: i have taken the time interval of 3sec.

.aspx page code:

 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
 <!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 runat="server">
    <title></title>
 </head>
 <body>
 <form id="form1" runat="server">
 <asp:Label ID="Label1" runat="server" Text="5"></asp:Label>
 <asp:Timer ID="Timer1" runat="server">
 </asp:Timer>
 <asp:ScriptManager ID="ScriptManager1" runat="server">
 </asp:ScriptManager>
 <asp:Button ID="Button1" runat="server" Text="Button" />
 <div>
 </div>
 </form>
 </body>
 </html>

VB.code

Partial Class Default2
Inherits System.Web.UI.Page
Dim i As Integer
Dim loopval As Integer

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    i = loopval - CDbl(Label1.Text)
    If i <> loopval Then
        Label1.Text = (CDbl(Label1.Text) - 1)
    Else
        MsgBox("display your message here")
        Timer1.Enabled = False
    End If

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Timer1.Enabled = True
    Timer1.Interval = 3000
    loopval = CDbl(Label1.Text)
End Sub
End Class

Hope this will help you :)

Upvotes: 0

ChitranjanThakur
ChitranjanThakur

Reputation: 91

This will help you:

C# code

protected void Page_Load(object sender, System.EventArgs e)
{
   int i = 0;
   int loopval = 0;

   Timer1.Enabled = true;
   Timer1.Interval = 1000;
   i = 0;
   loopval = Convert.ToDouble(Label1.Text);
      while (!(i >= loopval)) {
           Label1.Text = (Convert.ToDouble(Label1.Text) - 1);
           i = i + 1;
      }
   interaction.MsgBox("Display your message here");
   Timer1.Enabled = false;
}

VB code

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim i, loopval As Integer
    Timer1.Enabled = True
    Timer1.Interval = 1000
    i = 0
    loopval = CDbl(Label1.Text)
         Do Until i >= loopval
            Label1.Text = (CDbl(Label1.Text) - 1)
            i = i + 1
         Loop
    MsgBox("Display your message here")
    Timer1.Enabled = False
 End Sub

Dont forget to add ScriptManager control before timer control object.

do let me know if any error/ issue you will get.

Upvotes: 1

Related Questions