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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chandra24
    New Member
    • Feb 2010
    • 4

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

    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=serv er". No luck.

    Code:
    <html>
    <head>
    <script  language="javascript">
    function Print() {
          alert(document.getElementById('hdnMyHidden'));
          alert(document.getElementById('hdnMyHidden').value);
    
          var strmyCodes = document.getElementById("hdnmyCode").value;
          var strmyDesc = document.Form1.hdnmyCode.value;
          alert(strmyCodes);
          alert(strmyDesc);
    
          alert('hello from asp.net');
      }  
    
      </script>
    
    </head>
    <body scroll="no">
      <form id="Form1" method="post" runat="server">
        <input id="hdnmyCode" type="hidden" runat="server" name="hdnmyCode">
        <input id="hdnMyHidden" type="hidden" name="hdnMyHidden">    
        <input id="btn" type="submit" name="save" onclick="javascript:Print()">
        
      </form>
    </body>
    </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
    Last edited by Dormilich; Feb 18 '10, 03:07 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    try
    Code:
    <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.

    Comment

    • chandra24
      New Member
      • Feb 2010
      • 4

      #3
      Tried that, no luck.

      runat, attribute is used for accessing the variable in VB.NET server code.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        runat, attribute is used for accessing the variable in VB.NET server code.
        I guessed so far, still it’s not valid HTML code.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          tried the code myself and found the problem*.

          Code:
          var strmyDesc = document.[B]Form1[/B].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

          Comment

          • chandra24
            New Member
            • Feb 2010
            • 4

            #6
            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.

            Code:
            <html>
            <head>
            <script language="javascript">
            function Print() 
            {
            	alert(document.getElementById('hdnMyHidden'));
            	alert(document.getElementById('hdnMyHidden').value);
            
            	var strmyCodes = document.getElementById("hdnmyCode").value;
            	var strmyDesc = document.Form1.hdnmyCode.value;
            	alert(strmyCodes);
            	alert(strmyDesc);
            
            	alert('hello from asp.net');
            } 
            
            </script>
            
            </head>
            <body scroll="no">
            <form id="Form1" method="post" name="Form1">
            <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
            <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
            <input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
            
            <script type="text/javascript">
            <!--
            var theForm = document.forms['Form1'];
            if (!theForm) {
                theForm = document.Form1;
            }
            function __doPostBack(eventTarget, eventArgument) {
                if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
                    theForm.__EVENTTARGET.value = eventTarget;
                    theForm.__EVENTARGUMENT.value = eventArgument;
                    theForm.submit();
                }
            }
            // -->
            </script>
            
            
            <script language=JavaScript type=text/javascript> 
            alert('hello from vb.net');
            Print();
             </script>
            <input id="hdnmyCode" type="hidden" name="hdnmyCode" value="111">
            <input id="hdnMyHidden" type="hidden" name="hdnMyHidden"  value="hi 123"> 
            <input id="btn" type="submit" name="save" onclick="javascript: Print()">
            
            </form>
            </body>
            </html>

            Comment

            • chandra24
              New Member
              • Feb 2010
              • 4

              #7
              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 RegisterClientS criptBlock instead of RegisterStartup Script, to register and call this piece of Javascript. Although, the original copy of code was using RegisterStartup Script, which I had changed to RegisterClientS criptBlock for investigation.

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

              I really dont know whats the mystery behind. Simply, Old Wine in a New Bottle!

              Comment

              Working...