Solved

Hidden value is always null

Posted on 2013-11-13
10
304 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

809 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