473,403 Members | 2,222 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,403 software developers and data experts.

how to update automaticaly a javascript variable that is parse from the codebehind

Im using Anychart and I pretend to update the point value automatically after the Table1 is update in database.
Well Im new in ASP.net , and I expect that the javascript function should update the variable every time I update the table "Table1" in my database, what it does it updates the value just once.
I will apreciate your help.
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript" >
  2.             //<![CDATA[
  3.             var chart = new AnyChart('../anychart_files/swf/AnyChart.swf');
  4.             chart.setXMLFile('TemperatureXMLFile.xml');
  5.             chart.write('container');
  6.             var airtempjs = "<%=airtempcs%>"  // The string airtempcs is defined in codebehind
  7. function update() {
  8.         chart.updatePointData("Thermometer", "Temp", { value: (airtempjs) });
  9.                 }
  10.  
  11. setInterval(update, 400);     
  12.     //]]>
  13. </script>
//////////////////////////////////////////////
the code behind
Expand|Select|Wrap|Line Numbers
  1.  public  string airtempcs;
  2.  
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.             //Create the command and the Connections Objects
  6.         string connectionString = ConfigurationManager.ConnectionStrings["ChartTestConnectionString"].ConnectionString;
  7.         SqlConnection con = new SqlConnection(connectionString);
  8.         SqlCommand cmd = new SqlCommand("SELECT  AirTemp FROM Table1", con);
  9.  
  10.         //Open the connection and get the DataReader
  11.         con.Open();
  12.         SqlDataReader reader = cmd.ExecuteReader();
  13.  
  14.         while (reader.Read())
  15.         {
  16.             airtempcs = reader.GetDouble(0).ToString();
  17.         }
  18.  
  19.         reader.Close();
  20.         con.Close();
  21.  
  22.     }
Jan 10 '10 #1
6 3077
Frinavale
9,735 Expert Mod 8TB
You have to get the page to post back to the server in order to update the variable.
Are you using Ajax? (eg UpdatePanels?)

-Frinny
Jan 11 '10 #2
@Frinavale
Hi Frinny Thank you for your reply, but if i do a postback I am sendding the value of the javascrpit to the server ? What I realy want is get the value from the server side (code behind ) and this value should be update every time an update occuer in the data base. Im not using Ajax either UpdatePanels. What I guess everytime there is an update in the datbase the codebehind store the new updated value on the "public string airtempcs; " and than i can access this value in my javascript function . The reason Im doing the update in the Javascript is to see if i can access the" public string airtempcs; " is updated , I dont know what im doing wrong. I dont know if was clear in this explanation
Thank you again
Jan 11 '10 #3
Frinavale
9,735 Expert Mod 8TB
ASP.NET runs in a disconnected environment. That means that the browser sends a request to the server to the server, the server does some processing and the page is sent back to the browser. When the page is sent to the browser all server objects are destroyed. There is no persistent connection between the browser and the server.

This means that there is no way to be able to tell if there has been a change made to the database unless the browser makes a request to the server. This technique is called server polling.

Now, with regards to javascript: JavaScript runs in the browser and is unaware of the server just like the server code is unaware of what's running in the JavaScript.

One technique that you can use to transfer information between JavaScript and Server code is to use HiddenFields. Store the data in a HiddenField and you will be able to retrieve it in both environments.

The thing is that you're going to have to periodically send requests to the server to check if any changes have been made to the database.

You can use the JavaScript setTimeout method as a timer. This means that you can write a JavaScript method that submits the page to the server after a given amount of time has passed.

Or, you could use the Ajax Timer control....

-Frinny
Jan 11 '10 #4
gits
5,390 Expert Mod 4TB
just a note: the setTimeout() method is more likely used as a 'delay' method while the setInterval() method could be easyly used for periodically execution of code ...

kind regards
Jan 15 '10 #5
Frinavale
9,735 Expert Mod 8TB
Huh....thanks Gits!
I didn't know that the setInterval() method existed!

-Frinny
Jan 15 '10 #6
gits
5,390 Expert Mod 4TB
it's a quite handy method :)

kind regards
gits
Jan 15 '10 #7

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

Similar topics

4
by: Varun | last post by:
I want to access the variable in the codebehind in the javascript How can i do that Varun
5
by: Jim Banks | last post by:
Greetings I'm opening a pop up window with a html form, (in one document) and I want to pass a variable to the html form called from the hyperlink. Here's the code I'm using to pop up the...
6
by: Steve B. | last post by:
Hello everybody In a webpage, I use JS display data from an xml file and a xsl file: var data = new ActiveXObject("Microsoft.XMLDOM"); data.async = false; var dataUrl = "data.aspx";...
1
by: nospamjac | last post by:
Hi, Is there a way to update the text of an asp:label on a webform without refreshing the entire page? What is called by button clicks and other events that refresh a webform control? See the...
6
by: simon | last post by:
hello, what code would i use to kick off a javascript script after i had registered it? If (Not Page.IsClientScriptBlockRegistered("jsScript")) Then Page.RegisterClientScriptBlock("jsScript",...
4
by: UKuser | last post by:
Hi Guys, I am trying to create an editable table of a MySQL query where every field can be updated. My example script is at: http://nana46.coconia.net/test4.php however I am currently getting...
3
by: J055 | last post by:
Hi I have a PlaceHolder control inside a FormView EditItemTemplate: <asp:PlaceHolder ID="phResponseText" runat="server"> <tr> <td> <asp:Label ID="lblResponseText"...
5
by: HockeyFan | last post by:
We have an update panel that has a gridview with checkboxes and other items in each row. We went to the RowCreated event in the codebehind, to set an attribute on a checkbox in each row, to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.