locked
timer in asp RRS feed

  • Question

  • User-163969439 posted

    Hai,

    I am trying to build an online exam page that expires in a specified time. 

    Can any one help me how to setup a timer control that counts the time ranging from highest to lowest.

    Thank you.

    Thursday, October 31, 2013 3:15 PM

Answers

  • User-1635195291 posted

    Hi aravind.fz,

    You can try the below code or else you can follow the threads after the code to get more help:

    For .aspx Page:

    <asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Release">
            </asp:ScriptManager>
           
            <br />
            <br />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <contenttemplate>
                    <asp:Timer ID="Timer1" runat="server" Interval="10" ontick="Timer1_Tick">
                    </asp:Timer>
               
                    <br />
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                    <br />
                    <br />
                    <asp:Label ID="Label1" runat="server" Text="Text1"></asp:Label>
    
                </contenttemplate>
                <triggers>
                    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                </triggers>
            </asp:UpdatePanel>

    And in .cs file:

    public partial class PageTest : System.Web.UI.Page
        {
            TimeSpan tsTimeSpent;
            TimeSpan totaltime;
            string qry;
            DateTime dt;
            string[] dt1, dt2;
            int hour, min, sec1;
            TimeSpan tsD;
            static int i = 0, j = 0;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    ViewState["StartTime"] = hour + ":" + min + ":" + sec1;
    
                    tsD = new TimeSpan(0, 0, 0);
                    // tsTimeSpent = new TimeSpan(0,1, 1);
                }
    
                //      string ut = DateTime.Now.ToString("HH:mm:ss"); ;
    
    
                totaltime = new TimeSpan(0, 20, 0);
            }
            protected void Timer1_Tick(object sender, EventArgs e)
            {
    
    
                if (j == 59)
                {
                    i = i + 1;
                    j = 0;
                }
                string dt = DateTime.Now.ToString("HH:mm:ss");
                dt1 = dt.Split(':');
    
    
    
                hour = Convert.ToInt32(dt1[0].ToString());
                min = Convert.ToInt32(dt1[1].ToString());
                sec1 = Convert.ToInt32(dt1[2].ToString());
    
                tsTimeSpent = new TimeSpan(0, i, j);
                tsTimeSpent = tsTimeSpent.Subtract(tsD);
    
                tsTimeSpent = totaltime - tsTimeSpent;
                Label2.Text = tsTimeSpent.Hours.ToString() + ":" + tsTimeSpent.Minutes.ToString() + ":" + tsTimeSpent.Seconds.ToString();
                j = j + 1;
    
                if (i == 3)
                {
                    Label1.Text = "Text" + i.ToString() + "," + j.ToString();
                }
                else if (i == 6)
                {
                    Label1.Text = "Text" + i.ToString() + "," + j.ToString();
                }
                else if (i == 9)
                {
                    Label1.Text = "Text" + i.ToString() + "," + j.ToString();
                }
    
            }
        }

    Hope above code will get sorted all the things but for more help follow the links below:

    http://forums.asp.net/t/1388294.aspx/1

    http://www.codeproject.com/Tips/429419/Create-Count-Down-Timer-using-ASP-NET-Timer-Contro

    More Suggestions:

    Hope this helps.

    Thanks,

    Jatin




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 31, 2013 8:39 PM

All replies