title: assign value to a textbox obtained from a javascript function on page load
body: <p>Quick background: I am new to programming and have been using VisualStudio, ASP.NET, and C# for a couple months. I have no experience with JavaScript. I am trying to convert times from UTC to the time local to the browser viewing a page. After research I believe my best option for the various things I need to do is to detect the local offset from UTC on page load and then hold that information to use for local conversion and postback to the server. </p>
  
  <p>I have written the following js function in my "Source" page:</p>
  
  <pre><code>&lt;script type="text/javascript" &gt;
      function calculateOffset() {
          var rightNow = new Date();
          var UTCTime = rightNow.getUTCHours();
          var LocalTime = rightNow.getHours();
          var UTCDate = rightNow.getUTCDate();
          var LocalDate = rightNow.getDate();
          if (UTCDate == LocalDate) {
              var offset = LocalTime - UTCTime;
          }
          else {
              var offset = LocalTime - 24 - UTCTime;
          }
          return offset;
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>I then have a TextBox Control that I would like to use to hold the offset returned:</p>
  
  <pre><code>&lt;asp:TextBox ID="HiddenOffsetBox" runat="server" Visible="False" Text ="&lt;%# calculateOffset() %&gt;" &gt;&lt;/asp:TextBox&gt;
  </code></pre>
  
  <p>I receive the following error on debugging:
  Compiler Error Message: CS0103: The name 'calculateOffset' does not exist in the current context</p>
  
  <p>This is in a child page inheriting from a master page, I have tried placing the JS in both the head and body content, I have also created a .js file but haven't figured out how to call it. Primarily I am looking for a way to get the offset output from the JS function into the textbox but if you have any other suggestions I welcome them. Thanks ahead of time!</p>
  
  <p><strong>EDIT</strong>
  I created a test asp.net web form to try the suggestion below, to simplify everything here is the code:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="GTLWebApp.TestForm" %&gt;
  
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
      &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;head runat="server"&gt;
          &lt;script type="text/javascript" &gt;
  
          $(document).ready(function () {
          // recommend using jQuery to make sure the page is loaded
          document.getElementById('&lt;%= HiddenOffsetBox.ClientID%&gt;').text =   calculateOffset();
          //or
          //jQuery way
          //$('#&lt;%= HiddenOffsetBox.ClientID%&gt;').val(calculateOffset());
  
          function calculateOffset() {
              var rightNow = new Date();
              var UTCTime = rightNow.getUTCHours();
              var LocalTime = rightNow.getHours();
              var UTCDate = rightNow.getUTCDate();
              var LocalDate = rightNow.getDate();
              if (UTCDate == LocalDate) {
                  var offset = LocalTime - UTCTime;
              }
              else {
                  var offset = LocalTime - 24 - UTCTime;
              }
              return offset;
          }
          &lt;/script&gt;
          &lt;title&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form id="form1" runat="server"&gt;
          &lt;div&gt;
              &lt;asp:TextBox ID="HiddenOffsetBox" runat="server" Visible="True" Text = ""&gt;&lt;/asp:TextBox&gt;
          &lt;/div&gt;
          &lt;/form&gt;
  
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>When I run this I get no errors but I don't get the offset in the text box, any further help would be appreciated... </p>