title: Accessing ASP HiddenField in javascript
body: <p>I have been searching through here and google for a few days now, trying to figure out why I cannot get the value of a hiddenfield variable in javascript.  When accessed, the value is returned as undefined.<br>
  I have an ASP HiddenField inside an UpdatePanel which is part of a custom user control in a .aspx web page (standard issue).  </p>
  
  <p>In my user control, I need to get the .Value of the HiddenField (hdnServer) in javascript after setting it in C#.  But for some reason the following is not getting the correct value.  </p>
  
  <p>The MessageBox in the C# code returns the correct value (the code here has test values), but when accessed in javascript is undefined.  </p>
  
  <p>userControl.ascx:</p>
  
  <pre><code>//this function is called when the timer created in document.ready() elapses
  //returns the correct hdnServer value in the check. 
      var checkHdn = function () {
          var temp = document.getElementById("&lt;%=hdnServer.ClientID%&gt;").value;
          temp = temp.toString();
          if (temp != "") {
              $('#LoadingViewer').hide();
              clearInterval(checkSrv);
  
              //enable start button
              $('#startBtn').attr("Enabled", "true");
          }
      };
  
    function RdpConnect() {
  
                  //serverName = undefined here.  should be ip address when set in c# 
                  var serverName = document.getElementById("&lt;%= hdnServer.ClientID %&gt;").value;
                  alert(serverName);
                  if (serverName != "") {
                      MsRdpClient.Server = serverName;
                  }
              };
  </code></pre>
  
  <p>userControl.ascx.cs code-behind:</p>
  
  <pre><code>public partial class userControl : System.Web.UI.UserControl
      {
          System.Timers.Timer timer; 
  
          protected void Page_Load(object sender, EventArgs e)
          {
              timer = new System.Timers.Timer(5000);
              timer.Start();
          }
  
          protected void testOnTick(object sender, System.Timers.ElapsedEventArgs e)
          {
                  hdnServer.Value = "test value";
                  startBtn.Enabled = true;
                  timer.Enabled = false;
          }
      }
  </code></pre>
  
  <p>Here is the asp for HiddenField just in case: userControl.ascx:</p>
  
  <pre><code>&lt;asp:ScriptManager ID="ScriptManager1" runat="server" /&gt;
  &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always"&gt;
      &lt;Triggers&gt;
           &lt;!--trigger not used  --&gt;
         &lt;!-- &lt;asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /&gt;--&gt;
      &lt;/Triggers&gt;
      &lt;ContentTemplate&gt;
          &lt;asp:HiddenField ID="hdnServer" runat="server" /&gt;
          &lt;asp:Label ID="Label1" Text="Loading, please wait." CssClass="loading" runat="server"
              Font-Size="XX-Large" /&gt;
      &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p>Thank you for any advice in advance!</p>
  
  <p>EDIT:  messagebox removed..
  Here is rendered html:  <a href="http://pastie.org/3122247" rel="nofollow">http://pastie.org/3122247</a> </p>