John
John

Reputation: 285

Reading asp HiddenField in javascript

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?

Upvotes: 0

Views: 322

Answers (3)

vijay vangaveti
vijay vangaveti

Reputation: 94

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.

Upvotes: 0

Sharique Ansari
Sharique Ansari

Reputation: 544

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?

Upvotes: 4

JLane
JLane

Reputation: 514

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

Upvotes: 0

Related Questions