Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

Hidden value is always null

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
jssong2000
Asked:
jssong2000
  • 4
  • 4
  • 2
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
Hi
Try adding  .value
document.getElementById('hiddenisCheckedout').value;
       
Raj
From Mobile
0
 
Rajkumar GsSoftware EngineerCommented:
If not works try

document.getElementById('<%= hiddenisCheckedout.ClientID %>'').value;
0
 
jssong2000Author Commented:
It does not allow me to do this. Only valueof available on the list when I type "." Thanks!
0
Independent Software Vendors: 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!

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

Open in new window

0
 
jssong2000Author Commented:
field not found

so the hidden field has problem.
0
 
leakim971PluritechnicianCommented:
do a right click on the page in your web browser, choose view source and post it here
0
 
jssong2000Author Commented:
<!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
 
leakim971PluritechnicianCommented:
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
 
leakim971PluritechnicianCommented:
0
 
jssong2000Author Commented:
Great job. Appreciated!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now