0

In .NET 4.5...

I am trying to read this hidden field:

<asp:HiddenField ID="test2" runat="server" Value="" Visible="false" ClientIDMode="static"/>   

which the value is being set in the code behind here:

public static string TestSessionValue
        {
            get
            {
                object value = HttpContext.Current.Session["TestSessionValue"];
                return value == null ? "" : (string)value;
            }
            set
            {
                HttpContext.Current.Session["TestSessionValue"] = value;
            }
        }

TestSessionValue = String.Format("EmployeeCredential_ViewList.aspx?" + Employeeid + "={0}&" + StrIsadmin + "={1}", _empCredential.EmployeeId, IsAdmin);

test2.Value = TestSessionValue;

and then I am trying to read the value in javascript like so:

var hv = $('input[id$=test2]').val();

I have also tried this without success:

var hv = $('#test2').val();

How do I successfully read an asp HiddenField value in javascript?

John
  • 265
  • 2
  • 5
  • 16

3 Answers3

4

you need to remove Visible="false" then it will work or use this Visible="true"

So please Replace this:-

<asp:HiddenField ID="test2" runat="server" Value="" Visible="false" ClientIDMode="static"/>

with:-

<asp:HiddenField ID="test2" runat="server" Value="" Visible="true" ClientIDMode="static"/>

then try to get value either by

$("#test2").val()

or whatever you have written to get the value

Hope it will help?

Sharique Ansari
  • 534
  • 5
  • 12
  • 2
    Completely overlooked that, the server wont even render that control for the client. +1 – JLane Mar 18 '15 at 20:36
0

Get it using the ID:

    <script type="text/javascript">
        $(document).ready(function () {
            var hv= $('#test2').val();
        });
    </script>

More Info here: Get Value for Hidden Field

Community
  • 1
  • 1
JLane
  • 504
  • 2
  • 11
  • According to the debugger, hv is coming up as undefined. – John Mar 18 '15 at 20:05
  • I updated the post and to clarify, I have verified that test2 is getting set with the correct value in the code behind. – John Mar 18 '15 at 20:08
  • 1
    make sure the jquery runs after the page is done loading $(document).ready(function () { } – JLane Mar 18 '15 at 20:15
0

In your hiddenfield element you are using attribute Visible="false", It means hidden field will not render in to the webform, remove that attribute and try. anyways hiddenfield will not be visible in webform.