Solved

Java using a hidden value on an asp.net page

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

706 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

18 Experts available now in Live!

Get 1:1 Help Now