Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds


  Asked By: Aysel    Date: Feb 13    Category: Asp.net    Views: 1349

Alright, I have been avoiding this problem and decided
I better get it out of my hair. I am trying to dispaly
a running timer when the page is loader. I have built
a huge javascript string in my page load sub. The page
runs it just doesn't display the timer. Is this even a
good way to appoarch something like this or is there a
better way. I am up for any suggestions.

My page load sub routine is pasted below.

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ConnectString As String
MyConnect = New SqlConnection()
ConnectString =
MyConnect.ConnectionString = ConnectString
If Not IsPostBack Then

End If
Dim sScript As String
sScript = "<script language=JavaScript> "
sScript += "var(timerID = null)"
sScript += "var(timerRunning = False)"
sScript += "var(startDate)"
sScript += "var(startSecs)"
sScript += "function stopclock()"
sScript += "{"
sScript += "If (timerRunning) Then"
sScript += "clearTimeout(timerID)"
sScript += "timerRunning = False"
sScript += "}"
sScript += "function startclock()"
sScript += "{"
sScript += "startDate = New Date()"
sScript += "startSecs = (startDate.getHours()
* 60 * 60) + (startDate.getMinutes() * 60)"
sScript += "+ startDate.getSeconds()"
sScript += "stopclock()"
sScript += "showtime()"
sScript += "}"
sScript += "function showtime()"
sScript += "{"
sScript += "var(Now = New Date())"
sScript += "var(nowSecs = (Now.getHours() * 60
* 60) + (Now.getMinutes() * 60) + Now.getSeconds())"
sScript += "var elapsedSecs = nowSecs -
sScript += "var(hours = Math.Floor(elapsedSecs
/ 3600))"
sScript += "elapsedSecs = elapsedSecs - (hours
* 3600)"
sScript += "var(minutes =
Math.Floor(elapsedSecs / 60))"
sScript += "elapsedSecs = elapsedSecs -
(minutes * 60)"
sScript += "var(seconds = elapsedSecs)"
sScript += "var(TimeValue() = '' + hours)"
sScript += "timeValue += ((minutes < 10) ?
':0' : ':') + minutes"
sScript += "timeValue += ((seconds < 10) ?
':0' : ':') + seconds"
sScript += "// Update display"
'sScript +=
"document.timerForm.timerField.value = TimeValue()"
sScript += "document.form[0]." +
Lbl_Timer.ClientID + ".value = timeValue()"
sScript += "timerID = setTimeout('showtime()',
sScript += "timerRunning = True"
sScript += "}<"
sScript += "/script>"
If (Not
IsClientScriptBlockRegistered("TimerScript")) Then
End If
End Sub

<form runat="server">
<table bgcolor="#e5e5e5">
<td><asp:Label ID="Lbl_Timer"



13 Answers Found

Answer #1    Answered By: Faiza Mian     Answered On: Feb 13

I'd make another class, and wrap the timer  in that class.

Put a label in your aspx and attach the script to it in page  load

Label l = new Label()

Yournamespace.MyTimer tm = new Yournamespace.MyTimer();
Label.Text = tm.GetScript();

Really you should Register script but I haven't actually done that yet ... but
should be easy.
Or you could consider wrapping the script into a resources dll (don't ask me
how, I can only do images at teh moment)

Answer #2    Answered By: Felix Gray     Answered On: Feb 13

Thanks for the reply on this. I keep putting this task
on the back burner. I have never done anything like
you have suggested below with making another class and
wraping the timer  in that class. Could I have you
expound on this a little more for my novice self. It
would be greatly appreiciated.

Answer #3    Answered By: Sultana Tabassum     Answered On: Feb 13

just create a brand new class (soz ... I'm a sharper) to hold your
script string.
I actually went to all the effort to create a new project etc etc etc etc
etc to see exactly what you were on about, as maybe I had "found" a question
in your question that was not there and even missed one that was there.

Whatever - I'll stick with the version that tidies up your code. You'll
still have to register the script.
And the script itself doesn't work as its a translation/syntax cock up -
which isn't the question I take it ?

namespace YourNamespace
class myTimer
// then put all your strings in here
string sScript;
sScript = "<script language=JavaScript>\n";
sScript += "var(timerID = null);\n";
sScript += "var(timerRunning = False);\n";
sScript += "var(startDate);\n";
sScript += "var(startSecs);\n";
return sScript;

Then ... plonk a label in your aspx page  where you want to display  the
script, say Label1

Yournamespace.MyTimer tm = new Yournamespace.MyTimer();
Label1.Text = tm.GetScript();

Answer #4    Answered By: Hollie Hughes     Answered On: Feb 13


Answer #5    Answered By: Jackson Williams     Answered On: Feb 13

I guess I'm not catching on very quick. I Created the
new class. Now where do I register the script? What do
I declare tm as?

Answer #6    Answered By: Ethan Evans     Answered On: Feb 13

Remember I said you could hold the javascript  in a resource dll ... well
he's done just that.

Its perfect.

The code is in the ClientSideAdRotator ... internal class ClientScript

I am doing something with this lot at the moment and this is causing a
problem because there is another class called it somewhere else ( it won't
cause a problem  on its own).

When I have sorted it and isolated the code I'll post it ... but thank John
for it - its his code.

Answer #7    Answered By: Komal Mohammad     Answered On: Feb 13

I've set up a prj with both solutions. The syntax may be a bit awkward
for the VBers amoungst us .. but it's best to suss it out.

Just make a new web app under localhost and delete what's in WebForm1 and
replace it with the below

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.IO;

namespace WebApplication9876
class myJS
string js = "";
public myJS()
public string  getjs()
js += "<script>\n";
js += "alert('hello dan from class');\n";
js += "</script>\n";
return js;


/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
private void Page_Load(object sender, System.EventArgs e)
//true to get from class above, false to get from assembly
bool getfromclass = false;//true;
private void getScript(bool s)
string myjs = s ? RegisterClientScriptFromClass() :
if (!Page.IsClientScriptBlockRegistered("WebApplication9876.WebForm1"))
Page.RegisterClientScriptBlock("WebApplication9876.WebForm1", myjs);

protected string RegisterClientScriptFromClass()
WebApplication9876.myJS js = new WebApplication9876.myJS();
return js.getjs();

protected string RegisterClientScriptFromAssembly()
string js = "";
Assembly asm = Assembly.GetExecutingAssembly();
if (asm != null)
// create stream to the embedded file
//Stream stm = asm.GetManifestResourceStream(asm.GetName().Name + "." +
Stream stm = asm.GetManifestResourceStream(asm.GetName().Name +
StreamReader reader = new StreamReader(stm);
// read it in
js = reader.ReadToEnd();
// clean up
return js;

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.Load += new System.EventHandler(this.Page_Load);

In addition you will need to create a .js file and make sure you have set it
In that file put
alert('hello dan from assembly');

Just switch from true to false to get the script from teh different places.

//true to get from class above, false to get from assembly
bool getfromclass = false;//true;

Answer #8    Answered By: Chau Tran     Answered On: Feb 13

I am still
having a problem  though. First thing I'm a VBer, so
the c# is a little hard for me to transform. Second I
have created a project and was able to run the code
that Stephen sent me. Then I tried to put the script
in that I want to run. Now, here is my problem, the
line of code in my javascript  shown below. How do I
assign what the script is running  to Lbl_TimeCounter
in my aspx page? Does this make since?

js += "document.form[0]." + Lbl_TimeCounter.ClientID +
".value = timeValue";

Here is the script that I am using.

js = "<script language=JavaScript>\n";
js += "var timerID = null";
js += "var timerRunning = False";
js += "var startDate";
js += "var startSecs";
js += "function stopclock()";
js += "{";
js += "If (timerRunning) Then";
js += "clearTimeout(timerID)";
js += "timerRunning = False";
js += "}";
js += "function startclock()";
js += "{";
js += "startDate = New Date()";
js += "startSecs = (startDate.getHours() *
60 * 60) + (startDate.getMinutes() * 60)";
js += "+ startDate.getSeconds()";
js += "stopclock()";
js += "showtime()";
js += "}";
js += "function showtime()";
js += "{";
js += "var(Now = New Date())";
js += "var(nowSecs = (Now.getHours() * 60
* 60) + (Now.getMinutes() * 60) + Now.getSeconds())";
js += "var elapsedSecs = nowSecs -
js += "var(hours = Math.Floor(elapsedSecs
/ 3600))";
js += "elapsedSecs = elapsedSecs - (hours
* 3600)";
js += "var(minutes =
Math.Floor(elapsedSecs / 60))";
js += "elapsedSecs = elapsedSecs -
(minutes * 60)";
js += "var(seconds = elapsedSecs)";
js += "var(TimeValue() = '' + hours)";
js += "timeValue += ((minutes < 10) ?
':0' : ':') + minutes";
js += "timeValue += ((seconds < 10) ?
':0' : ':') + seconds";
js += "// Update display";
js += "document.form[0]." +
Lbl_TimeCounter.ClientID + ".value = timeValue";
js += "timerID = setTimeout('showtime()',
js += "timerRunning = True";
js += "}</script>";
return js;

Answer #9    Answered By: Viheke Fischer     Answered On: Feb 13

document.getElementById("Lbl_Timer").innerHTML = "jjj";

so try
js += "document.form[0]." + Lbl_TimeCounter.ClientID +
> ".innerHTML = timeValue";

I don't know if you are but don't assign this value to the same label
the script is attached to !
(though it might be interesting to see if it works ... it might as the
script will be held in mem ! dunno about functions though)

Answer #10    Answered By: Jeanette Greene     Answered On: Feb 13

I don't understand what you are saying about assigning
value to the same label the script is attached to?

This is what my HTML looks like.

<form id="Form1" method="post" runat="server">
<asp:label id="Lbl_TimeCounter" style="Z-INDEX:
101; LEFT: 8px; POSITION: absolute; TOP: 8px"

Answer #11    Answered By: Isabella Campbell     Answered On: Feb 13

I meant if the script wasn't registered.
i.e. you'd likely be adding the string  to a label, in which case my argument

I've never actually registered a script and have only ever added a script to
a label. When I only saw one label in your code I assumed you may be
overwriting it with your timer  value.

Answer #12    Answered By: Logan Bouchard     Answered On: Feb 13

Can anybody suggest me how to make timer  control
work in ASP.Net using C#.I tried out but i could not
get the expected results.Actually i want the time to
be displayed along with the ticks on a label control
on web page.i.e it should be displayed as ,suppose
2:05:06 here 2 represents hours, 05 represents minutes
and 06 represents seconds and the seconds part should
keep on changing as 06 ,07 ,08 and so on.I also tried
to embed Javascript within ASP.Net application but it
did not work out

Answer #13    Answered By: Aidyn Smith     Answered On: Feb 13

ajax timer  control from

has Timer Control docs

Didn't find what you were looking for? Find more on Timer Or get search suggestion and latest updates.