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.

<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

Recommended Answers

All 4 Replies

What you posted was asp code not html. My suggestion is that you bring up the web page and do a View Source in your browser. The javascript is probably right. Your fields are probably empty when they hit the browser. Which would mean you have an asp error not a javascript error.

from view state:

<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 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" runat="server" 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>

Well I do see a problem. Some of your javascript is in the body and is running before your input fields are loaded.

Here your javascript call to Print() is running before the input fields are loaded.

<script language=JavaScript type=text/javascript>
alert('hello from vb.net');
Print();
</script>
<input id="hdnmyCode" type="hidden" runat="server" name="hdnmyCode" value="111">
<input id="hdnMyHidden" type="hidden" name="hdnMyHidden" value="hi 123">
<input id="btn" type="submit" name="save" onclick="javascript: Print()">

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.