Click here to Skip to main content
15,867,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I was trying to implement a countdown timer using ASP.NET. It is working fine until displaying the remaining minutes.

The code in Default.aspx page is
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Task3.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            position: relative;
            top: 33px;
            left: 314px;
            width: 470px;
            height: 193px;
        }

        .auto-style2 {
            width: 202px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="SM1" runat="server"></asp:ScriptManager>
            <asp:Timer ID="timer1" runat="server"
                Interval="1000" OnTick="timer1_tick" Enabled="False">
            </asp:Timer>
        </div>
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Text="Enter Number of Minutes:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" TextMode="Number" Height="28px" Width="191px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2"></td>
                <td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>
                    <div>
                        <asp:UpdatePanel ID="updPnl"
                            runat="server" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:Label ID="lblTimer" runat="server" Font-Size="X-Large"></asp:Label>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="timer1" EventName="tick" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </div>
                    </td>
            </tr>
            <tr>
                <td class="auto-style2"></td>
                <td>
                    <asp:Label ID="lblresult" runat="server"></asp:Label>
                </td>
            </tr>
        </table>

    </form>
</body>
</html>


The code in default.aspx.cs code is

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Task3
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }
        protected void timer1_tick(object sender, EventArgs e)
        {
            if (0 > DateTime.Compare(DateTime.Now,
            DateTime.Parse(Session["timeout"].ToString())))
            {
                lblTimer.Text = "Number of Minutes Left: " +
                ((Int32)DateTime.Parse(Session["timeout"].
                ToString()).Subtract(DateTime.Now).TotalMinutes).ToString();
            }
            else
            {

                lblresult.Text = "Countdown Complete";
                lblresult.ForeColor = System.Drawing.Color.Green;
                timer1.Enabled = false;

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled=true;
            if (!SM1.IsInAsyncPostBack)
            {
                int time = int.Parse(TextBox1.Text)+1;
                Session["timeout"] = DateTime.Now.AddMinutes(time).ToString();

            }
        }

    }
}



The problems here is

1. The else loop is not working. When I placed a braking point I fount it is going to page load each time.

2. How to display the number of seconds remaining

Thank you
Posted
Comments
Andreas Gieriet 3-Jan-15 4:26am    
What is the value of your timeout attribute?
Andi
Member 11239022 3-Jan-15 4:52am    
It is secified by the user.
Here I kept a textbox for user to enter
Andreas Gieriet 3-Jan-15 7:41am    
If this is a user entry, any garbage can be entered.
You should make the parsing of the timeout robust - like parsing of any entry from the user as well as from the environment, etc.
If this is in your given case the problem is not clear since you provide too little data in your question.
Also to consider: a timeout is usually rater a timespan than a datetime, i. e. a duration rather than a point in time.
I suggest that you provide a method that does robust parsing of the user entry. Only if a valid value is parsed, you proceed in the program - else do decent errorhandling.
BTW: there is no such thing like an "else loop". This is called an "else branch" or "else path". A loop is something that iterates - i.e. that loops.
If the current solution works and you want to get the seconds instead of the minutes, the respective calculation should be trivial e.g. by replacing the calll to get the minutes by a call to get the seconds.
Cheers
Andi
Each time it will go to page load, but after that it should go inside the timer click. Debug and see.
Member 11239022 3-Jan-15 4:53am    
Yes It is going to timer tick event and also it is going into the else condition but the label text inside the else loop is not working.

1 solution

can you please look into this article

Create Count Down Timer using ASP.NET Timer Control and Ajax[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900