whoah
whoah

Reputation: 4443

How to send dynamically value from javascript to hiddenfield

I have a variable "place", which is changing dynamically (get diffrent values for some actions). Here JS code.

google.maps.event.addListener(autocomplete, 'place_changed', function () {
  var place = autocomplete.getPlace();
});

How to get this variable to asp.net(C#) in code behind?

Upvotes: 0

Views: 289

Answers (1)

Adam Plocher
Adam Plocher

Reputation: 14233

document.getElementById('myHiddenField').value = place;

Just keep in mind that client IDs are dynamic in ASP.NET, unless you're using ASP.NET 4 and set ClientIDMode = "Static" on that hidden field. So you may need to add something like this to your Page_Load:

Page.RegisterStartupScript(this, "hiddenFieldIdSetter", "var hiddenFieldID = '"+ myHiddenField.ID +"';", true);

and then use the following javascript, instead:

document.getElementById(hiddenFieldID).value = place;

Upvotes: 1

Related Questions