Monday, 5 August 2013

How to set Remaining time on our Web Application in Asp.Net with TimeSpan …..


How to set Remaining time  on our Web Application  in Asp.Net with TimeSpan …..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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 runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
height: 23px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</td>
<td align="right">
<i><font color="green">Total Time&nbsp;&nbsp;&nbsp; </font></i><asp:Label ID="lbltotaltime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td align="right" class="style2">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
<i><font color="maroon">Remaining Time&nbsp;&nbsp;&nbsp; </font></i>  &nbsp;<asp:Label ID="lblremainingtime" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>

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


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

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//this is the total time of  Examination from where exam will be started.........
TimeSpan ts = new TimeSpan(03, 00, 00);
Session["time"] = ts;
lbltotaltime.Text = ts.ToString();
}

}
protected void Timer1_Tick(object sender, EventArgs e)
{
//this is the ending time of  Examination  where exam will be ended.........
TimeSpan tt = new TimeSpan(02, 59, 00);
TimeSpan tk = (TimeSpan)Session["time"];
if (tk != tt)
{
//this is time in second which is decreasing(Subtracting) into the starting time.....
TimeSpan ti = new TimeSpan(00, 00, 01);
tk = tk.Subtract(ti);
Session["time"] = tk;

lblremainingtime.Text = tk.ToString();
}
else
{
Response.Redirect("ExamOver.aspx");
}

}
}



1 comments: