Solved

Hidden value is always null

Posted on 2013-11-13
10
299 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

706 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

23 Experts available now in Live!

Get 1:1 Help Now