2012-11-20 60 views
1

我一直在尋找答案在這裏和其他地方,但到目前爲止還沒有找到如何做到這一點。隱藏字段在代碼背後

我有一個隱藏字段的定義如下在我的.aspx頁面中:

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

我在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;  
    } 
} 

基本上我只是想設置一次值。

現在我想在後面的代碼來檢查值:

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

End If 

然而,儘管我已經驗證該值在JS功能被設置,它始終是一個空字符串時,它得到到我的代碼背後。

任何人看到我在做什麼錯了?

+0

請不要寫'== true'。 – Ryan

+0

請確認您未在頁面加載中設置隱藏字段的值。 – cbillowes

回答

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

屬性是value而不是valueOf。 (以前也不是undefined;只需檢查!document.forms[0].hfAddressChange.value。)