Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hidden value is always null

Posted on 2013-11-13
10
Medium Priority
?
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 total points
ID: 39645818
0
 

Author Closing Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

719 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