Rachel F
Rachel F

Reputation: 61

ASP.NET Hidden field's OnValueChange

I have several different scenarios, all with different longitudes and latitudes (and other data, but that's beside the point) in JSON. I am parsing the JSON ok, and have been able to get the desired values. What I would like to do is transfer these values to the CodeBehind. What I am doing so far is something like this:

This is the responsible script:

function getScenarioDetails(json) {      
    $("#Welcome").text("Welcome user " + json.user);   
    $("#longitude").val(json.current_loc.lon).change();       
    $("#latitude").val(json.current_loc.lat).change();     
}

And these are the hidden fields:

<form runat="server">
   <asp:HiddenField runat="server" id="longitude" OnValueChanged="valueChanged"   />
   <asp:HiddenField runat="server" id="latitude" OnValueChanged="valueChanged"  />  
</form>

I realise that the value is being changed. But something is going wrong with the OnValueChanged as it is not firing. What exactly am I doing wrong?

Upvotes: 3

Views: 3511

Answers (1)

Cesar Loachamin
Cesar Loachamin

Reputation: 2738

I will try to explain this, First the client id is generated based on the NamingContainer parents so if your hidden fields are nested in a container you should use the property ClientIDMode and set the value to Static to ensure the client id is the same in your script.

The ValueChange event is fired when a control cause a postback so if you put a button that has the onclick event it cause a postack and the ASP.Net lifecycle starts executing the ValueChange event for your hidden inputs, Some controls as DropDownList has a property AutoPostBack when it set to true it makes a postback as soon the javascript change event happens, so it not wait until a postback occurs. The HiddeField doesn't have a AutoPostBack property so if you really need to postack after you change the values you could make a postback so:

function getScenarioDetails(json) {      
    $("#Welcome").text("Welcome user " + json.user);   
    $("#longitude").val(json.current_loc.lon);       
    __doPostBack('longitude', ''); 
    .....
}

Upvotes: 4

Related Questions