?
Solved

Java using a hidden value on an asp.net page

Posted on 2009-04-14
5
Medium Priority
?
201 Views
Last Modified: 2012-05-06
Experts,

What am I doing wrong here?

I need javascript to access a hidden field value.

Here is a stripped down version of my page that doesnt work.

(I just want the alert to show the hidden value)

Please help!
<%@ Page Language="VB" %>
 
<script runat="server">
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' only do this the first time the page loads
        
        If Page.IsPostBack = False Then
         
        Hidden1.Value = "The new Hidden Value"
        Response.Write(Hidden1.Value)
         
        End If
    
        
    End Sub
    
 
    
</script>
 
 
 
 
 
  
    <script type="text/javascript">
 
        var MyHVal=document.getElementById('Hidden1').value;
        alert("The hidden value is - "+MyHVal);
      
    </script>
 
<input id="Hidden1" type="hidden" name="myhiddenvalue" runat="server" value= "this is the hidden value"/>

Open in new window

0
Comment
Question by:Real_coffee
[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
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24138168
>>I need javascript to access a hidden field value.

JS runs on the client. Is that where you want to access it?
0
 
LVL 12

Accepted Solution

by:
Gibu George earned 2000 total points
ID: 24138339
The problem is the hidden field is create after your script tag, move it above like this

<input id="Hidden1" type="hidden" name="myhiddenvalue" runat="server" value= "this is the hidden value"/>

<script type="text/javascript">
 
        var MyHVal=document.getElementById('Hidden1').value;
        alert("The hidden value is - "+MyHVal);
     
    </script>
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24139015
Try this:
    <script type="text/javascript">
 
        var MyHVal=document.getElementById('<%=Hidden1.ClientID>%').value;
        alert("The hidden value is - "+MyHVal);
      
    </script>

Open in new window

0
 

Author Comment

by:Real_coffee
ID: 24139235
Hi Prairiedog - the suggested code change got me -  The server block is not well formed. on line 35?

Gibu George got it though - i guess the script couldnt find the hidden value.

Thanks guys!



0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24139299
Sorry, there is a typo. It should be "%>", not ">%". My solution should work no matter where you define your HiddenField. But anyway, you got you solution.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

770 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