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?

1
  • What version of .NET are you using? That affects how the ClientIDMode works. Commented Mar 18, 2015 at 20:17

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Completely overlooked that, the server wont even render that control for the client. +1
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

3 Comments

According to the debugger, hv is coming up as undefined.
I updated the post and to clarify, I have verified that test2 is getting set with the correct value in the code behind.
make sure the jquery runs after the page is done loading $(document).ready(function () { }
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.