By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,968 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,968 IT Pros & Developers. It's quick & easy.

Strange problem with hidden variables being found 'null' by Javascript

P: 4
Javascript is not recognising any of the hidden variables on this web form. It simply reports them to be null. I tried declaring a new variable without "runat=server". No luck.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script  language="javascript">
  4. function Print() {
  5.       alert(document.getElementById('hdnMyHidden'));
  6.       alert(document.getElementById('hdnMyHidden').value);
  7.  
  8.       var strmyCodes = document.getElementById("hdnmyCode").value;
  9.       var strmyDesc = document.Form1.hdnmyCode.value;
  10.       alert(strmyCodes);
  11.       alert(strmyDesc);
  12.  
  13.       alert('hello from asp.net');
  14.   }  
  15.  
  16.   </script>
  17.  
  18. </head>
  19. <body scroll="no">
  20.   <form id="Form1" method="post" runat="server">
  21.     <input id="hdnmyCode" type="hidden" runat="server" name="hdnmyCode">
  22.     <input id="hdnMyHidden" type="hidden" name="hdnMyHidden">    
  23.     <input id="btn" type="submit" name="save" onclick="javascript:Print()">
  24.  
  25.   </form>
  26. </body>
  27. </html>
When queried the value, it says "Object required" and when queried the variable, it says "null".

Environment :
VS2008, ASP.NET, VB.NET
.NET 2.0/3.5
IE 7.0
IIS 5
WinXP SP2
Feb 18 '10 #1
Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
try
Expand|Select|Wrap|Line Numbers
  1. <input id="btn" type="button" name="save" onclick="Print()">
this won’t submit the form, but it will execute the JavaScript.

PS. runat is not a valid HTML attribute.
Feb 18 '10 #2

P: 4
Tried that, no luck.

runat, attribute is used for accessing the variable in VB.NET server code.
Feb 19 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
runat, attribute is used for accessing the variable in VB.NET server code.
I guessed so far, still itís not valid HTML code.
Feb 19 '10 #4

Dormilich
Expert Mod 5K+
P: 8,639
tried the code myself and found the problem*.

Expand|Select|Wrap|Line Numbers
  1. var strmyDesc = document.Form1.hdnmyCode.value;
Form1 is an invalid accessor. the form requires this as a name attribute, to be accessed this way (an ID wonít do there).

* - Hooray for Firebug
Feb 19 '10 #5

P: 4
Actually, the code i posted above is the source code. When it is run with VB.NET and the values are substituted by the server code, "runat" goes off. So, the "name" attribute gets assigned. This all can be seen when we "view source" the rendered html page.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. function Print() 
  5. {
  6.     alert(document.getElementById('hdnMyHidden'));
  7.     alert(document.getElementById('hdnMyHidden').value);
  8.  
  9.     var strmyCodes = document.getElementById("hdnmyCode").value;
  10.     var strmyDesc = document.Form1.hdnmyCode.value;
  11.     alert(strmyCodes);
  12.     alert(strmyDesc);
  13.  
  14.     alert('hello from asp.net');
  15.  
  16. </script>
  17.  
  18. </head>
  19. <body scroll="no">
  20. <form id="Form1" method="post" name="Form1">
  21. <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
  22. <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
  23. <input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
  24.  
  25. <script type="text/javascript">
  26. <!--
  27. var theForm = document.forms['Form1'];
  28. if (!theForm) {
  29.     theForm = document.Form1;
  30. }
  31. function __doPostBack(eventTarget, eventArgument) {
  32.     if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
  33.         theForm.__EVENTTARGET.value = eventTarget;
  34.         theForm.__EVENTARGUMENT.value = eventArgument;
  35.         theForm.submit();
  36.     }
  37. }
  38. // -->
  39. </script>
  40.  
  41.  
  42. <script language=JavaScript type=text/javascript> 
  43. alert('hello from vb.net');
  44. Print();
  45.  </script>
  46. <input id="hdnmyCode" type="hidden" name="hdnmyCode" value="111">
  47. <input id="hdnMyHidden" type="hidden" name="hdnMyHidden"  value="hi 123"> 
  48. <input id="btn" type="submit" name="save" onclick="javascript: Print()">
  49.  
  50. </form>
  51. </body>
  52. </html>
  53.  
  54.  
Feb 22 '10 #6

P: 4
As I said it was a strange problem and yes it was indeed.

I finally took a brand new ASP.NET page (.aspx) and kept adding html and javascript in bits and pieces from the original. So did I add the server code in the (aspx.vb) file. At a point, I was facing the same problem, with the hidden controls again reporting null.

In the code behind (vb), I was writing RegisterClientScriptBlock instead of RegisterStartupScript, to register and call this piece of Javascript. Although, the original copy of code was using RegisterStartupScript, which I had changed to RegisterClientScriptBlock for investigation.

But, at the end, with the new files, all same old code (HTML & server code) and even the original RegisterStartupScript, it worked.

I really dont know whats the mystery behind. Simply, Old Wine in a New Bottle!
Mar 3 '10 #7

Post your reply

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