473,413 Members | 1,989 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,413 software developers and data experts.

JavaScript Postback blues...

gchq
96
Hi there

I am attempting to show a stopwatch within a TextBox - the script starts to fire, but as soon as the page posts back it stops, then returns to zero! I'm sure there is a simple explanation, but........

I seem to think the onclick event fires the 'findTIME' event and it starts running, but then the postback event loads the full script again and resets the counter. Placing it in the 'is not page.IsPostBack' block still causes the same issue, then of course it is no longer loaded - sigh!

Here is the javascript:-

Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
  2.  
  3.         Dim vTimer As String = "<script language='javascript'>" & _
  4.                                    "var base = 60; " & _
  5.                                    "var clocktimer,dateObj,dh,dm,ds,ms;" & _
  6.                                    "var readout='';" & _
  7.                                    "var h=1;" & _
  8.                                    "var m=1;" & _
  9.                                    "var tm=1;" & _
  10.                                    "var s=0;" & _
  11.                                    "var ts=0;" & _
  12.                                    "var ms=0;" & _
  13.                                    "var show=true;" & _
  14.                                    "var init=0;" & _
  15.                                    "var mPLUS=new Array(" & _
  16.                                            "'m0'," & _
  17.                                           " 'm1'," & _
  18.                                           " 'm2'," & _
  19.                                           " 'm3'," & _
  20.                                           " 'm4'," & _
  21.                                           " 'm5'," & _
  22.                                           " 'm6'," & _
  23.                                           " 'm7'," & _
  24.                                           " 'm8'," & _
  25.                                           " 'm9'" & _
  26.                                    ");" & _
  27.                                    "var ii=0;" & _
  28.                                    "function startTIME() { " & _
  29.                                    "var cdateObj = new Date(); " & _
  30.                                    "var t = (cdateObj.getTime() - dateObj.getTime())-(s*1000);" & _
  31.                                    "if (t>999) { s++; } " & _
  32.                                    "if (s>=(m*base)) { " & _
  33.                                    "    ts=0;" & _
  34.                                    "  m++; " & _
  35.                                    "    } else {" & _
  36.                                    "    ts=parseInt((ms/100)+s);" & _
  37.                                    "    if(ts>=base) { ts=ts-((m-1)*base); }" & _
  38.                                    "    }" & _
  39.                                    "if (m>(h*base)) { " & _
  40.                                    "    tm=1;" & _
  41.                                    "    h++;" & _
  42.                                    "    } else {" & _
  43.                                    "    tm=parseInt((ms/100)+m);" & _
  44.                                    "    if(tm>=base) { tm=tm-((h-1)*base); }" & _
  45.                                    "    }" & _
  46.                                    "ms = Math.round(t/10); " & _
  47.                                    "if (ms>99) {ms=0;}" & _
  48.                                    "if (ms==0) {ms='00';}" & _
  49.                                    "if (ms>0&&ms<=9) { ms = '0'+ms; }" & _
  50.                                    "if (ts>0) { ds = ts; if (ts<10) { ds = '0'+ts; }} else { ds = '00'; } " & _
  51.                                    "dm=tm-1;" & _
  52.                                    "if (dm>0) { if (dm<10) { dm = '0'+dm; }} else { dm = '00'; }" & _
  53.                                    "dh=h-1;" & _
  54.                                    "if (dh>0) { if (dh<10) { dh = '0'+dh; }} else { dh = '00'; }" & _
  55.                                    "readout = dh + ':' + dm + ':' + ds + '.' + ms; " & _
  56.                                    "if (show==true) { document.form1.clock.value = readout; }" & _
  57.                                    "clocktimer = setTimeout('startTIME()',1); " & _
  58.                                    "}" & _
  59.                                    "function findTIME() {" & _
  60.                                    "if (init==0) {" & _
  61.                                    "    dateObj = new Date();" & _
  62.                                    "    startTIME();" & _
  63.                                    "    init=1;" & _
  64.                                    "    } else {" & _
  65.                                    "    if(show==true) {" & _
  66.                                    "        show=false;" & _
  67.                                    "        } else {" & _
  68.                                    "        show=true;" & _
  69.                                    "        }" & _
  70.                                    "    }" & _
  71.                                    "}" & _
  72.                                    "</script>"
  73.         Page.ClientScript.RegisterStartupScript(Me.GetType(), "vTimer", vTimer)
  74.  
  75.     End Sub
and this is fired by two Button objects

Expand|Select|Wrap|Line Numbers
  1. If Not Page.IsPostBack Then
  2.             clock.Text = "00:00:00:00"
  3.             Button1.Attributes.Add("onclick", "findTIME()")
  4.             Button2.Attributes.Add("onclick", "findTIME()")
  5.         End If
I've tried loading the script a number of ways but same problem! Any help would be very much appreciated!
Apr 4 '07 #1
2 1126
gchq
96
I've tried a number of ways of resolving this - having said that JavaScript and C# have always been a bit alien to me, unlike VB - but to no avail.

I did try adding this code:-

Expand|Select|Wrap|Line Numbers
  1. "function exscript()" & _
  2.                                        "{" & _
  3.                                        "findTIME();" & _
  4.                                        "return false;" & _
  5.                                        "}" & _
Then modified the onclick event to:-

Expand|Select|Wrap|Line Numbers
  1. Button1.Attributes.Add("onclick", "exscript()")
In a vain attempt to stop the rest of the JavaScript executing again.

If anyone has any idea on where I'm going wrong I'd appreciate being pointed in the right direction.....
Apr 5 '07 #2
gchq
96
Workaround

This may not be the best way as it does to an extent defeat server postbacks, but by adding :-

Expand|Select|Wrap|Line Numbers
  1. Dim vTime As String = "<script> language='javascript'>" & _
  2.         "function CurrentTime() { " & _
  3.         "var now = new Date(); " & _
  4.         "var hour = now.getHours(); " & _
  5.         "var minute = now.getMinutes(); " & _
  6.         "var second = now.getSeconds(); " & _
  7.         "if (hour < 10) { hour = '0' + hour; }" & _
  8.         "if (minute < 10) { minute = '0' + minute; }" & _
  9.         "if (second < 10) { second = '0' + second; }  " & _
  10.         "document.getElementById('TextBox6').innerText = hour + ':' + minute + ':' + second; " & _
  11.         "document.getElementById('Button1').disabled=true;" & _
  12.         "document.getElementById('Button2').disabled=false;" & _
  13.         "}" & _
  14.         "</script>"
  15.         Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "vTime", vTime)
and

Expand|Select|Wrap|Line Numbers
  1. Dim vTime2 As String = "<script> language='javascript'>" & _
  2.                 "function CurrentTime2() { " & _
  3.                 "var now = new Date(); " & _
  4.                 "var hour = now.getHours(); " & _
  5.                 "var minute = now.getMinutes(); " & _
  6.                 "var second = now.getSeconds(); " & _
  7.                 "if (hour < 10) { hour = '0' + hour; }" & _
  8.                 "if (minute < 10) { minute = '0' + minute; }" & _
  9.                 "if (second < 10) { second = '0' + second; }  " & _
  10.                 "document.getElementById('TextBox8').innerText = hour + ':' + minute + ':' + second; " & _
  11.                 "document.getElementById('Button2').disabled=true;" & _
  12.                 "}" & _
  13.                 "</script>"
  14.         Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "vTime2", vTime2)
This now changes the server side functions I wanted to client side, a few other controls had to be amended this way as well. Then...

Expand|Select|Wrap|Line Numbers
  1. Button1.Attributes.Add("onclick", "findTIME(); CurrentTime(); return false;")
  2.             Button2.Attributes.Add("onclick", "findTIME(); CurrentTime2(); return false;")
It now runs!
Apr 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
12
by: Mark Fox | last post by:
Hello, I am attempting to do something very simple. I have a page MainPage.aspx and a popup window Popup.aspx. When users click on the linkbutton in the popup window I am looking to do some...
2
by: Bruce W.1 | last post by:
There's something I can't figure out. I added some javascript behavior to my datagrid, just like in this article, except in C# instead of VB: ...
21
by: Martin Eyles | last post by:
I am trying to get javascript to cause a page to post back. I have tried calling _doPostBack from my script, but generates an error "object expected". I think this is because the page's script...
2
by: Alan Silver | last post by:
Hello, I have just discovered that if you turn off Javascript, then cross-page posting does not work. The postback goes to the originating page, which basically means that nothing happens. I...
4
by: =?Utf-8?B?T3Bh?= | last post by:
Hi, I'm not sure if the title is correct. Here's what I am having trouble with: I have a server side button control where I am calling a javascript function via the OnClientClick property. I...
8
by: Deft.Jab | last post by:
This problem only exists in IE, due to UI restrictions I'm using a href in an error label to bring up a modal popup. I'm using a callback to filter a grid on the popup. The problem: The href...
4
by: Peter | last post by:
ASP.NET I have an application which use ASP.NET Autocomplete extender which works great. But I have a question how to update all the fields on the screen using Ajax. Users starts typing in a...
2
Frinavale
by: Frinavale | last post by:
JavaScript in ASP.NET Using JavaScript in ASP.NET pages can make your application seem to work faster and prevent unnecessary calls to the server. JavaScript can be used to perform client-side...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.