在代码背后获取隐藏字段

时间:2022-08-25 21:49:21

I have been looking at answers here and other places but so far have not found exactly how to do this.

我一直在寻找这里和其他地方的答案,但到目前为止还没有找到如何做到这一点。

I have the following definition for a hidden field in my .aspx page:

我的.aspx页面中隐藏字段的定义如下:

<asp:HiddenField ID="hfAddressChange" runat="server" />

I set the value in a javascript function on the client:

我在客户端的javascript函数中设置了值:

function confirmAddressChange()
{
    if (typeof document.forms[0].hfAddressChange.valueOf ==="undefined")
    { 
     var res = (confirm('"Update Contact Addresses to Rich?"')==true);
     document.forms[0].hfAddressChange.valueOf = res;    
    }
}

Basically I only want to set the value once.

基本上我只想设置一次值。

Now I want to check the value in the code behind:

现在我想检查后面代码中的值:

If hfAddressChange.Value <> String.Empty Then
    Dim x As String = "Do Something here"

End If

However, even though I have verified that the value IS being set in the js function, it is always an empty string when it gets to my code behind.

但是,即使我已经验证了在js函数中设置了值IS,当它到达我的代码后,它总是一个空字符串。

Anyone see what the heck I'm doing wrong?

有人看到我做错了什么?

1 个解决方案

#1


1  

document.forms[0].hfAddressChange.valueOf = res;

The property is value, not valueOf. (And it won't be undefined earlier, either; just check !document.forms[0].hfAddressChange.value.)

该属性是值,而不是valueOf。 (并且它也不会在之前被定义;只需检查!document.forms [0] .hfAddressChange.value。)

#1


1  

document.forms[0].hfAddressChange.valueOf = res;

The property is value, not valueOf. (And it won't be undefined earlier, either; just check !document.forms[0].hfAddressChange.value.)

该属性是值,而不是valueOf。 (并且它也不会在之前被定义;只需检查!document.forms [0] .hfAddressChange.value。)