2014-05-08 69 views
-6

我開發在線考試系統爲學生asp.net web應用程序。 學生在不同的科目進行測試。但我無法實現定時器功能。實施倒計時器在ASP.NET C#的在線考試系統

我爲每個測試分配了30分鐘。 請給出解決方案,30分鐘後頁面重定向到其他頁面,即結果頁面。 也可以在分鐘和秒鐘的網頁上顯示倒數計時器。

回答

0

來實現這一功能使用jQuery倒數計時器,因爲它是輕量級的,沒有對服務器沒有額外的負載只是它運行在客戶端。 請參閱本插件Jquery Countdown Plugin。 希望能滿足您的需求。

+0

Thanks..It好插件.. –

0

我只有VB.Net。試着將它轉換爲C#。

在VB.Net代碼:

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 

上的.aspx代碼(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> 
1

如果你想使其難以欺騙系統,該服務器將具有強制執行30分鐘的限制來發布對任何給定頁面的響應。否則,接受測試的人可以執行諸如刷新頁面或禁用JavaScript來規避時間限制等事情。

要實現所需的UI功能(即不允許學生在30分鐘後提交頁面,只是爲了讓服務器端拒絕它太晚),只需使用JavaScript定時器,該定時器一旦到期就重定向。

您也不妨包括頁面上的倒計時小工具,爲方便學生。網絡上有很多這樣的例子。