且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改ASP.NET文件中的隐藏字段值并在javascript中获取新值

更新时间:2023-02-11 21:13:33

最有可能,它可能是UpdatePanel问题。在选择行时,您的隐藏字段可能无法更新。


您好Sanjay



我得到了解决方案。

隐藏字段也需要在客户端初始化,即在javascript中。所以在这段代码之后
 hdnPaxID.Value = string.Join(,,lstCabins.Select(n => n.Passenger)); 

我调用了javascript函数将更新隐藏字段值

 function setHiddenFieldValue(hdnPaxID,hdnPaxValue)
{
document.getElementById(hdnPaxID)。 value = hdnPaxValue;
}





所以在我的主要javascript函数中,我可以获得这些更新的值。

结论:如果在客户端使用隐藏字段值,那么它只需要在客户端进行初始化。



我不知道它是如何发生的但它对我有用


Hello

I have declared one hidden field in ASP.NET and initialize its value in code behind file during selection of grid row.
Then I have updated this hidden field in a javascript for that selected row.
Now when I moved to next row, the hidden value gets changed from code behind file. But in javascript file I am getting the same value which was updated for previous row.

C#:

hdnPaxID.Value = string.Join(",", lstCabins.Select(n => n.Passenger));



Javascript:

var hdnFieldValue = document.getElementById('hdnPaxID').value;
//Some code to update hidden field.



Kindly suggest.
Thanks!

Most probably, it may be UpdatePanel issue. Your hidden field may not get updated during selection of rows.


Hi Sanjay

I got the solution.
The hidden fields need to initialize at client side also i.e., in javascript. So after this code
hdnPaxID.Value = string.Join(",", lstCabins.Select(n => n.Passenger));

I've called javascript function which will update hiddenfield value

function setHiddenFieldValue(hdnPaxID, hdnPaxValue)
{
    document.getElementById(hdnPaxID).value = hdnPaxValue;
}



So in my main javascript function, I can get these updated value.
Conclusion: If hiddenfield value to be use at client side then it needs to initialize at client side only.

I don't know how it happens but it works for me.