Solved

Java using a hidden value on an asp.net page

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

937 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