Laziale
Laziale

Reputation: 8225

How to set asp.net hidden field in JavaScript and access the value in C# code behind

I'm trying to set hidden field value in JS which I think works fine with this approach:

  <asp:HiddenField ID="hfLatSW" runat="server" />

   var latSW = bounds.getSouthWest().lat();
   $('#<%=hfLatSW.ClientID %>').value = latSW;

I am trying to access the value on asp.net button click in code behind gives me null value. What might be happening in the postback and why am I not able to access the value set by JavaScript?

Upvotes: 0

Views: 7951

Answers (1)

Jason P
Jason P

Reputation: 27012

.value isn't a jQuery property. Use .val():

$('#<%=hfLatSW.ClientID %>').val(latSW);

Or without jQuery:

document.getElementById('<%=hfLatSW.ClientID %>').value = latSW;

Upvotes: 3

Related Questions