To achieve this functionality use jquery countdown timer as it is lightweight and there is no extra load on server simply it runs on client side. please refer to this plugin Jquery Countdown Plugin. Hope this full fill you need.
Implementing Countdown Timer in ASP.NET c# for Online Test System [closed]
-
17-07-2023 - |
Question
I am developing a asp.net web application for Online Test System for students. Where students take test in different subjects . But i unable to implement the timer functionality.
I allotted 30 minutes for each test. Please give solution that after 30 minutes page redirects to other page i.e result page. Also show the countdown timer on webpage with minutes and seconds.
La solution 2
Autres conseils
If you want to make it difficult to cheat the system, the server will have to enforce the 30 minute limit to post a response to any given page. Otherwise, the person taking the test can do things like refresh the page or disable JavaScript to circumvent the time restriction.
To achieve the desired UI functionality (that is, not allow the student to submit the page after 30 minutes, only to have the server side reject it as too late), simply use a JavaScript timer that redirects once it expires.
You may also wish to include a countdown widget on the page, as a convenience to the student. There are numerous examples of that on the web.
I only have VB.Net. Just try to convert it to C#.
On VB.Net code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'timer
If Not Me.IsPostBack Then
Dim timerStartValue As Long = 1000
Me.timerStartValue = 120000 ' 2 minutes
Me.TimerInterval = 1000
End If
End Sub
'=== TIMER FUNCTIONS ==='
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim timerVal As String = Request.Form("timerData")
If Not String.IsNullOrEmpty(timerVal) Then
timerVal = timerVal.Replace(",", String.Empty) ' will always be here on postback
Me.timerStartValue = Long.Parse(timerVal)
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim bldr As New Text.StringBuilder()
bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", Me.timerStartValue, Me.TimerInterval, Me.lblTimerCount.ClientID)
bldr.Append("Timer.go()")
ClientScript.RegisterStartupScript(Me.GetType(), "TimerScript", bldr.ToString(), True)
' used to persist current value
ClientScript.RegisterHiddenField("timerData", timerStartValue)
End Sub
Private Property TimerInterval() As String
Get
Dim o As Object = ViewState("timerInterval")
If Not Nothing Is o Then
Return Integer.Parse(o.ToString())
End If
Return 50 ' default
End Get
Set(ByVal value As String)
ViewState("timerInterval") = value
End Set
End Property
On .aspx code (javascript):
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function myTimer(startVal,interval,outputId, dataField){
this.value = startVal;
this.OutputCntrl = document.getElementById(outputId);
this.currentTimeOut = null;
this.interval = interval;
this.stopped=false;
this.data = null;
var formEls = document.form1.elements
if(dataField){
for(var i=0; i < formEls.length -1; i++){
if(formEls[i].name == dataField){
this.data = formEls[i];
i = formEls.length + 1;
}
}
}
myTimer.prototype.go = function (){
if(this.value > 0 && this.stopped == false){
this.value = (this.value - this.interval);
if(this.data){
this.data.value = this.value;
}
var current = this.value;
this.OutputCntrl.innerHTML = "0" + this.Minutes(current) + ':' + this.Seconds(current);
this.currentTimeOut = setTimeout("Timer.go()", this.interval);
}
else {
$("#timesUp").dialog({
title: "Time is up!",
open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
modal: true,
draggable: false,
resizable: false
});
}
}
myTimer.prototype.stop = function (){
this.stopped = true;
if(this.currentTimeOut != null){
clearTimeout(this.currentTimeout);
}
}
myTimer.prototype.Hours = function(value){
return Math.floor(value/3600000);
}
myTimer.prototype.Minutes = function(value){
return Math.floor((value - (this.Hours(value)*3600000))/60000);
}
myTimer.prototype.Seconds = function(value){
var hoursMillSecs = (this.Hours(value)*3600000)
var minutesMillSecs = (this.Minutes(value)*60000)
var total = (hoursMillSecs + minutesMillSecs)
var ans = Math.floor(((this.value - total)%60000)/1000);
if(ans < 10)
return "0" + ans;
return ans;
}
}
</script>