Solved

Hidden value is always null

Posted on 2013-11-13
10
302 Views
Last Modified: 2013-11-14
Hi Expert,

I created a HiddenField control:
 <asp:HiddenField runat="server" ID="hiddenisCheckedout" />


And I set value to the hidden field from code behind:

if (isCheckedout())
{
     hiddenisCheckedout.Value = "true";
}
else
{
     hiddenisCheckedout.Value = "false";
}


And then I retrieve the value in Javascript:
        var isChked = document.getElementById('hiddenisCheckedout');
        alert(isChked);

But isChked is always null. It shoud be the value I setup from code behind. Please help.

Appreciated!!
0
Comment
Question by:jssong2000
  • 4
  • 4
  • 2
10 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 39645503
Hi
Try adding  .value
document.getElementById('hiddenisCheckedout').value;
       
Raj
From Mobile
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 39645516
If not works try

document.getElementById('<%= hiddenisCheckedout.ClientID %>'').value;
0
 

Author Comment

by:jssong2000
ID: 39645518
It does not allow me to do this. Only valueof available on the list when I type "." Thanks!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39645526
try this :
var isChked = document.getElementById('<%= hiddenisCheckedout.ClientID %>');
alert( isChked ? isChked.value : "field not found" );

Open in new window

0
 

Author Comment

by:jssong2000
ID: 39645599
field not found

so the hidden field has problem.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 82

Expert Comment

by:leakim971
ID: 39645601
do a right click on the page in your web browser, choose view source and post it here
0
 

Author Comment

by:jssong2000
ID: 39645784
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form method="post" action="Default.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="izwqc5WQZyDCR+BuJMRPJMse5OYwgnLKs8wVFArRTZAwVacizcc/aBGavK5C7Qa7GU74j1XrkyiwlC8OWUN2/iWUAZ4AlUY4IC1lqDL4Ykw=" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="Ol14+jgIk+FbIeTSdI5ltslouQ0rO/ZO3WcKopuYsoxYGB+63ej8hyE7uMYBFO6Dirz+VvnpC+wvSgAbgZVSKVEsGJ+TMsdV+pSp41hK2GWbH9cIhnmvNmQTQTIUNnhmSZOTKD1bP8jRIiLUvjMaAA==" />
</div>
               <script type="text/javascript">
                   var isChked = document.getElementById('hiddenisCheckedout');
                   alert(isChked ? isChked.value : "field not found");



                   function getCookie(c_name) {
                       var c_value = document.cookie;
                       var c_start = c_value.indexOf(" " + c_name + "=");
                       if (c_start == -1) {
                           c_start = c_value.indexOf(c_name + "=");
                       }
                       if (c_start == -1) {
                           c_value = null;
                       }
                       else {
                           c_start = c_value.indexOf("=", c_start) + 1;
                           var c_end = c_value.indexOf(";", c_start);
                           if (c_end == -1) {
                               c_end = c_value.length;
                           }
                           c_value = unescape(c_value.substring(c_start, c_end));
                       }
                       return c_value;
                   }
                   function setCookie(c_name, value, exdays) {
                       var exdate = new Date();
                       exdate.setDate(exdate.getDate() + exdays);
                       var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
                       document.cookie = c_name + "=" + c_value;
                   }

                   //alert('test1 = ' + getCookie('test1'));
                   //setCookie('test1', null, 1);
                   //alert('test1 again ==' + getCookie('test1'));


               </script>
    <div>
    
    </div>
        <p>
            Hello</p>
        <p>
            &nbsp;</p>
        <input type="submit" name="Button1" value="Button" id="Button1" />
         <input type="hidden" name="hiddenisCheckedout" id="hiddenisCheckedout" value="false" />
    </form>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39645812
replace :
      var isChked = document.getElementById('hiddenisCheckedout');
                   alert(isChked ? isChked.value : "field not found");
by :
window.onload = function() {
      var isChked = document.getElementById('hiddenisCheckedout');
                   alert(isChked ? isChked.value : "field not found");
}

or put your script at the end of the page
else you can't get a reference of a field not already present in the page, the html of field is loaded after the script and it's execution

using window.onload wait the complete loading of the page
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39645818
0
 

Author Closing Comment

by:jssong2000
ID: 39648565
Great job. Appreciated!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now