简体   繁体   中英

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM