Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java using a hidden value on an asp.net page

Posted on 2009-04-14
5
Medium Priority
?
207 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

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!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

598 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