简体   繁体   English

ASP.NET隐藏字段的OnValueChange

[英]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. 我有几种不同的场景,在JSON中都有不同的经度和纬度(以及其他数据,但这不是重点)。 I am parsing the JSON ok, and have been able to get the desired values. 我正在解析JSON ok,并且能够获得所需的值。 What I would like to do is transfer these values to the CodeBehind. 我想要做的是将这些值传递给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. 但OnValueChanged出现了问题,因为它没有触发。 What exactly am I doing wrong? 我究竟做错了什么?

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. 我将尝试解释这一点,首先,客户端ID是基于NamingContainer父项生成的,因此如果您的隐藏字段嵌套在容器中,则应使用属性ClientIDMode并将值设置为Static以确保客户端ID在您的内容中是相同的脚本。

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. 当控件导致回发时会触发ValueChange事件,所以如果你把一个带有onclick事件的按钮导致一个postack并且ASP.Net生命周期开始为你的隐藏输入执行ValueChange事件,DropDownList的一些控件有一个属性AutoPostBack当它设置为true时,它会在javascript更改事件发生后立即进行回发,因此它不会等到发生回发。 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: HiddeField没有AutoPostBack属性,因此如果您在更改值后确实需要进行后备包,则可以进行回发:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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