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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • breckherbytes
    New Member
    • Jan 2010
    • 2

    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.
    Code:
    <script type="text/javascript" language="javascript" >
    			//<![CDATA[
    		    var chart = new AnyChart('../anychart_files/swf/AnyChart.swf');
    		    chart.setXMLFile('TemperatureXMLFile.xml');
    		    chart.write('container');
    		    var airtempjs = "<%=airtempcs%>"  // The string airtempcs is defined in codebehind
    function update() {
            chart.updatePointData("Thermometer", "Temp", { value: (airtempjs) });
            	    }
    
    setInterval(update, 400); 	
    	//]]>
    </script>
    //////////////////////////////////////////////
    the code behind
    Code:
     public  string airtempcs;
    
        protected void Page_Load(object sender, EventArgs e)
        {
                //Create the command and the Connections Objects
            string connectionString = ConfigurationManager.ConnectionStrings["ChartTestConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("SELECT  AirTemp FROM Table1", con);
    
            //Open the connection and get the DataReader
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
    
            while (reader.Read())
            {
                airtempcs = reader.GetDouble(0).ToString();
            }
    
            reader.Close();
            con.Close();
    
        }
    Last edited by Frinavale; Jan 11 '10, 01:39 PM. Reason: Please post code in [code]...[/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • breckherbytes
      New Member
      • Jan 2010
      • 2

      #3
      Originally posted by Frinavale
      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
      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

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          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

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Huh....thanks Gits!
            I didn't know that the setInterval() method existed!

            -Frinny

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              it's a quite handy method :)

              kind regards
              gits

              Comment

              Working...