473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Timer reset on page back/page forward

I have spent way too much time trying to solve the following problem:

I have a datalist with a timer in the footer template. It works wonderfully
except when the user pages back, then forward, and the counter is reset. I
saw in one of Kevin Spencer's comments not to put the timer in javascript on
the load function, but I have to get the script to run before the page is
rendered, otherwise my timer is not displayed. I have a flag "Hidden Test
Staus" that I try to set at first in page_load, and then reset in the
javascript.

I am getting desperate to understand what to do.

This is my code:

In the code behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlConnection1.Open()
If IsPostBack = False Then
SetUpQuestions()
RegisterHiddenField("HiddenTestStatus", "Y")
End If

Dim HddenFld As String = Request.Form("HiddenField")
If HddenFld = "1" Then
Me.SaveEmpResponses()
Me.CalculateScore()
Me.DeleteEmployeeResponses()
Response.Redirect("frmSurveyInstruction.aspx", False)
End If
End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
If IsPostBack = False Then
Dim strScript As String
strScript = "<script language=""javascript"">"
strScript = strScript + "var textBeforeCountdown=""You have "";"
strScript = strScript + "var textAfterCountdown="" to complete this test
...."";"
strScript = strScript + "if (document.Form1.HiddenTestStatus.value=""Y"")
var seconds = 600;"
strScript = strScript + "else var seconds =
document.Form1.HiddenFieldTime.value;"
strScript = strScript + "document.Form1.HiddenTestStatus.value=""N"";"
strScript = strScript + "var secondsAfterAlert = 2;"
strScript = strScript + "var timer;"
strScript = strScript + "function countDown() {"
strScript = strScript + "if (seconds>=60){ "
strScript = strScript + "minutes=""""+seconds/60;"
strScript = strScript + "minutes=minutes.substring(0,1);"
strScript = strScript + "if (minutes!=""1"") mPlural=""s"";"
strScript = strScript + "else mPlural="""";"
strScript = strScript + "minutes2=minutes+"" minute""+mPlural+"" and ""; "
strScript = strScript + "seconds2=seconds-minutes*60;"
strScript = strScript + "if (seconds2!=1) seconds2=seconds2+"" seconds"";"
strScript = strScript + "else seconds2=seconds2+"" second""; "
strScript = strScript +
"timerSpan.innerHTML=textBeforeCountdown+minutes2+ seconds2+textAfterCountdown; "
strScript = strScript + "}"
strScript = strScript + "else{"
strScript = strScript + "if (seconds!=1) seconds2=seconds+"" seconds""; "
strScript = strScript + "else seconds2=seconds+"" second""; "
strScript = strScript +
"timerSpan.innerHTML=textBeforeCountdown+seconds2+ textAfterCountdown; "
strScript = strScript + "}"
strScript = strScript + "if(seconds ==0) {"
strScript = strScript + "stopTimer();"
strScript = strScript + "}"
strScript = strScript + "seconds--;"
strScript = strScript + "document.Form1.HiddenFieldTime.value = seconds;"
strScript = strScript + "if(seconds >=0)
timer=window.setTimeout(""countDown()"",1000);"
strScript = strScript + "}"
strScript = strScript + "function stopTimer() {"
strScript = strScript + "clearTimeout(timer); "
strScript = strScript + "document.Form1.HiddenField.value = ""1"";"
strScript = strScript + "alert(""Your time is up. Please click 'OK' to
continue to the next assessment""); "
strScript = strScript + "timerSpan.innerHTML=""You have completed your
time!!!"";"
strScript = strScript + "document.Form1.submit()"
strScript = strScript + "}"
strScript = strScript + "countDown()"
strScript = strScript + "</script>"
RegisterStartupScript("BeginTest", strScript)
End If
End Sub
In the html:

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datalist id="dlQuestions" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 32px"
runat="server" BorderWidth="1px" GridLines="Both" CellPadding="4"
BackColor="White" BorderStyle="None"
BorderColor="#CC9966" DataKeyField="QuestNum" DataMember="Questions"
Height="328px" Width="950px"
OnItemDataBound="dlQuestions_ItemDataBound">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<HeaderTemplate>
<asp:Label id="Label1" runat="server" Height="42px" Font-Bold="True"
Font-Size="Medium">SHIPLEY INSTITUTE OF LIVING SCALE</asp:Label>
</HeaderTemplate>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<FooterTemplate>
<asp:Button id="btnFinish" onclick="btnFinish_Click" runat="server"
Text="Finish"></asp:Button>
<span id="timerSpan"></span>
</FooterTemplate>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<ItemTemplate>
<P>
<table border="0" cellspacing="10" cellpadding="5" bordercolor="black">
<tr>
<td width='750'>
<asp:Label id=lblQuestNum runat="server" Height="30px"
Width="100px" Font-Bold="False" Text='<%# "(" &
DataBinder.Eval(Container.DataItem,"QuestNum") & " of 40) " %>'
Font-Size="XX-Small">
</asp:Label>
<asp:Label id=lblQuestDescr runat="server" Height="30px"
Width="120px" Font-Bold="True" Text='<%#
DataBinder.Eval(Container.DataItem,"QuestDescr") %>' Font-Size="XX-Small">
</asp:Label>
<asp:RadioButtonList id="Rbl" runat="server" Width="500px"
Height="30px" Font-Size="XX-Small" DataTextField="ResponseDescr"
DataValueField="ResponseNum" RepeatLayout="Flow"
RepeatDirection="Horizontal"></asp:RadioButtonList>
</td>
</tr>
</table>
</P>
</ItemTemplate>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
</asp:datalist>
<asp:label id="lblErrorMessage" style="Z-INDEX: 102; LEFT: 200px;
POSITION: absolute; TOP: 8px"
runat="server" ForeColor="Red" Font-Bold="True"></asp:label>
<input id="HiddenField" type="hidden" name="HiddenField" runat="server">
<input id="HiddenFieldTime" type="hidden" name="HiddenFieldTime"
runat="server">
</form>
Jan 24 '06 #1
1 2062
Your question is confusing to read (to me), and I didn't follow it well, but
I can tell you why the JavaScript timer isn't working. You're using the
JavaScript setTimeout function to call your countDown function. The
setTimeout function only executes one time. You need to use the JavaScript
setInterval function if you want to continually call the countDown function.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"jmgro" <jm***@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
I have spent way too much time trying to solve the following problem:

I have a datalist with a timer in the footer template. It works
wonderfully
except when the user pages back, then forward, and the counter is reset.
I
saw in one of Kevin Spencer's comments not to put the timer in javascript
on
the load function, but I have to get the script to run before the page is
rendered, otherwise my timer is not displayed. I have a flag "Hidden Test
Staus" that I try to set at first in page_load, and then reset in the
javascript.

I am getting desperate to understand what to do.

This is my code:

In the code behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlConnection1.Open()
If IsPostBack = False Then
SetUpQuestions()
RegisterHiddenField("HiddenTestStatus", "Y")
End If

Dim HddenFld As String = Request.Form("HiddenField")
If HddenFld = "1" Then
Me.SaveEmpResponses()
Me.CalculateScore()
Me.DeleteEmployeeResponses()
Response.Redirect("frmSurveyInstruction.aspx", False)
End If
End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
If IsPostBack = False Then
Dim strScript As String
strScript = "<script language=""javascript"">"
strScript = strScript + "var textBeforeCountdown=""You have "";"
strScript = strScript + "var textAfterCountdown="" to complete this test
..."";"
strScript = strScript + "if (document.Form1.HiddenTestStatus.value=""Y"")
var seconds = 600;"
strScript = strScript + "else var seconds =
document.Form1.HiddenFieldTime.value;"
strScript = strScript + "document.Form1.HiddenTestStatus.value=""N"";"
strScript = strScript + "var secondsAfterAlert = 2;"
strScript = strScript + "var timer;"
strScript = strScript + "function countDown() {"
strScript = strScript + "if (seconds>=60){ "
strScript = strScript + "minutes=""""+seconds/60;"
strScript = strScript + "minutes=minutes.substring(0,1);"
strScript = strScript + "if (minutes!=""1"") mPlural=""s"";"
strScript = strScript + "else mPlural="""";"
strScript = strScript + "minutes2=minutes+"" minute""+mPlural+"" and ""; "
strScript = strScript + "seconds2=seconds-minutes*60;"
strScript = strScript + "if (seconds2!=1) seconds2=seconds2+"" seconds"";"
strScript = strScript + "else seconds2=seconds2+"" second""; "
strScript = strScript +
"timerSpan.innerHTML=textBeforeCountdown+minutes2+ seconds2+textAfterCountdown;
"
strScript = strScript + "}"
strScript = strScript + "else{"
strScript = strScript + "if (seconds!=1) seconds2=seconds+"" seconds""; "
strScript = strScript + "else seconds2=seconds+"" second""; "
strScript = strScript +
"timerSpan.innerHTML=textBeforeCountdown+seconds2+ textAfterCountdown; "
strScript = strScript + "}"
strScript = strScript + "if(seconds ==0) {"
strScript = strScript + "stopTimer();"
strScript = strScript + "}"
strScript = strScript + "seconds--;"
strScript = strScript + "document.Form1.HiddenFieldTime.value = seconds;"
strScript = strScript + "if(seconds >=0)
timer=window.setTimeout(""countDown()"",1000);"
strScript = strScript + "}"
strScript = strScript + "function stopTimer() {"
strScript = strScript + "clearTimeout(timer); "
strScript = strScript + "document.Form1.HiddenField.value = ""1"";"
strScript = strScript + "alert(""Your time is up. Please click 'OK' to
continue to the next assessment""); "
strScript = strScript + "timerSpan.innerHTML=""You have completed your
time!!!"";"
strScript = strScript + "document.Form1.submit()"
strScript = strScript + "}"
strScript = strScript + "countDown()"
strScript = strScript + "</script>"
RegisterStartupScript("BeginTest", strScript)
End If
End Sub
In the html:

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datalist id="dlQuestions" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 32px"
runat="server" BorderWidth="1px" GridLines="Both" CellPadding="4"
BackColor="White" BorderStyle="None"
BorderColor="#CC9966" DataKeyField="QuestNum" DataMember="Questions"
Height="328px" Width="950px"
OnItemDataBound="dlQuestions_ItemDataBound">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<HeaderTemplate>
<asp:Label id="Label1" runat="server" Height="42px" Font-Bold="True"
Font-Size="Medium">SHIPLEY INSTITUTE OF LIVING SCALE</asp:Label>
</HeaderTemplate>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<FooterTemplate>
<asp:Button id="btnFinish" onclick="btnFinish_Click" runat="server"
Text="Finish"></asp:Button>
<span id="timerSpan"></span>
</FooterTemplate>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<ItemTemplate>
<P>
<table border="0" cellspacing="10" cellpadding="5"
bordercolor="black">
<tr>
<td width='750'>
<asp:Label id=lblQuestNum runat="server" Height="30px"
Width="100px" Font-Bold="False" Text='<%# "(" &
DataBinder.Eval(Container.DataItem,"QuestNum") & " of 40) " %>'
Font-Size="XX-Small">
</asp:Label>
<asp:Label id=lblQuestDescr runat="server" Height="30px"
Width="120px" Font-Bold="True" Text='<%#
DataBinder.Eval(Container.DataItem,"QuestDescr") %>' Font-Size="XX-Small">
</asp:Label>
<asp:RadioButtonList id="Rbl" runat="server" Width="500px"
Height="30px" Font-Size="XX-Small" DataTextField="ResponseDescr"
DataValueField="ResponseNum" RepeatLayout="Flow"
RepeatDirection="Horizontal"></asp:RadioButtonList>
</td>
</tr>
</table>
</P>
</ItemTemplate>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
</asp:datalist>
<asp:label id="lblErrorMessage" style="Z-INDEX: 102; LEFT: 200px;
POSITION: absolute; TOP: 8px"
runat="server" ForeColor="Red" Font-Bold="True"></asp:label>
<input id="HiddenField" type="hidden" name="HiddenField" runat="server">
<input id="HiddenFieldTime" type="hidden" name="HiddenFieldTime"
runat="server">
</form>

Jan 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
6
by: Antti Laakso | last post by:
Hi i have function like above Public Sub halytystutkinta() Dim ds As New DataSet ds = dl2.HaeHalytys() Dim onkohal As Int16 onkohal = ds.Tables(0).Rows(0).Item("onkohalytys") halid =...
7
by: Noozer | last post by:
I have a timer on a form. It isn't firing at all. I know that the timer is enabled, and that the interval is low (4000, which should be 4 seconds). To ensure the timer wasn't being inadvertantly...
7
by: Mike Eaton | last post by:
Hi All, I have a simple application that allows users to clock in and out and stores the data for use by the payroll department. It spends most of its life as a tray icon and when the user...
3
by: John Salerno | last post by:
I'd be curious to know if this works any differently on other computers/platforms or while other things are happening in the background. I can't tell if it's the Timer object that isn't keep...
4
by: Bails | last post by:
Hi Im an absolute beginner in programming and am using VB.Net Express. To start my larning I decided to do a "Real World" app instead of "hello world" and am creating a Poker Countdown clock. ...
11
by: Hotrod2000 | last post by:
I'm quite new to programming but I'm having problems getting a timer to work in visual studio.net I've created a timer on a form, enabled it and then typed the following code (from the mdsn...
9
by: MC | last post by:
I would like to display a timer in the corner of the page that shows the user how many minutes:seconds till the session times out. Are there any good examples out there? Google has again failed...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.