Solved

Java using a hidden value on an asp.net page

Posted on 2009-04-14
5
190 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
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

816 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

10 Experts available now in Live!

Get 1:1 Help Now