Monday, 21 July 2014

Set timer for online Exam

No comments
.ASPX

 <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_click">
        </asp:Timer>

 CS

  protected void Page_Load(object sender, EventArgs e)
{
       DataTable dt = bl.bind_Details();
        DataRow drows = dt.Rows[0];
        string timeing = drows["Exam_time"].ToString();
        string[] s = timeing.Split(new char[] { ':' });
        int hours = s[0];
        int min = s[1];
        int lastmin = Convert.ToInt16(min) - 1;
        secound = 60;
}

protected void Timer1_click(object sender, EventArgs e)
    {

        Session["sec"] = DateTime.Now.AddSeconds(secound);
        Session["min"] = DateTime.Now.AddMinutes(minute);
        Session["hour"] = DateTime.Now.AddHours(hour);

        TimeSpan tsec = new TimeSpan();
        TimeSpan tmin = new TimeSpan();
        TimeSpan thour = new TimeSpan();

        tsec = (DateTime)Session["sec"] - DateTime.Now;
        tmin = (DateTime)Session["min"] - DateTime.Now;
        thour = (DateTime)Session["hour"] - DateTime.Now;

        secound = (int)tsec.TotalSeconds;
        minute = (int)tmin.TotalMinutes;
        hour = (int)thour.TotalHours;

        if (secound <= 0)
        {
            if (hour == 0 && minute == 0)

                secound = 0;
            else
            {
                secound = 60;
                minute -= 1;
            }
        }
        if (minute <= 0)
        {
           if (hour  < 0 && minute < 0)
            {
                hour = minute = secound = 0;

            }
            else
            {
                if (hour == 0 && minute == 0)
                    hour = minute = 0;

                if (hour > 0 && minute < 0)
                {
                    minute = 59;
                    hour -= 1;
                }
            }
        }
        if (minute <= -1 || hour <= -1)
        {
            if (hour <= -1)
            {
                minute = 0;
                hour += 1;
            }
            else
                minute -= 1;
            secound = 0;
            minute += 1;

        }
        else

            if (secound > 0)
                secound -= 1;

        if (hour == 0 && minute == 0 && secound == 0)
        {
            submit_answer();
            result();
            Response.Redirect("TimeUp.aspx");                  
        }
        lbltime.Text = hour + " : " + minute + " : " + secound;
    }