2011-08-10 21 views
4

我有3個ASP.NET文本框和一個HiddenField。 第三個文本框的值(禁用)取決於其他兩個值。檢索禁用的ASP.NET文本框的值

的公式是

txtPricepad = txtPrice/txtCarton

<asp:TextBox ID="txtPriceCase" runat="server" onblur="javascript:GetPricePerPad();></asp:TextBox> 
<asp:TextBox ID="txtCarton" runat="server"></asp:TextBox> 
<asp:TextBox ID="txtPricePad" Enabled="false" runat="server" ></asp:TextBox> 
<asp:HiddenField ID="hdPricepad" runat="server"/> 

    function GetPricePerPad() 
    { 
    var priceCase = document.getElementById('ctl00_content_txtPriceCase').value; 
    var cartons  = document.getElementById('ctl00_content_txtCarton').value; 
    var res   = Number(priceCase)/Number(cartons); 

    document.getElementById('ctl00_content_txtPricePad').value = res; 
    document.getElementById('ctl00_content_hdPricepad').value = res; 
    } 

假設txtPricePad的初始值是0,並且txtCarton爲12 當txtPrice的值改變爲1200,GetPricePerPad()將被調用,因此txtPricePad將爲100.

Javascript成功更改了txtPricePad的值爲100,但是當我從代碼隱藏中調用txtPricePad時,其值仍然爲0. 這就是爲什麼我將公式的結果分配給HiddenField的原因。還有其他方法可以做到這一點嗎?我不想再次使用HiddenField。

+0

可能重複的[我想獲得在我們的下一個jsp中禁用的文本框的值,但我得到空值](http://stackoverflow.com/questions/3757806/i-want-to-獲得值的禁用文本框在我們的下一個jsp但我是得到NU) –

回答

10

Liz,是否可以使文本字段爲只讀(ReadOnly = True),而不是使其成爲Disabled = True?原因是,當禁用文本字段時,不會與POST請求中的表單一起提交。 See this question.

如果你想讓它看起來好像被禁用,我想你可以將CssClass應用到按鈕。

+0

我認爲這是一個偉大的建議,謝謝! – Liz

+2

我知道這是一年前,但試圖檢索一個禁用的文本框的價值全天8小時浪費 - 您的答案工作 –

2

我會用的2個選項

  1. 執行從其它輸入
  2. 使用JavaScript啓用形式txtPricePad領域提交再次計算服務器端的一個,見下面

    var pricePad = document.getElementById(<%= txtPricePad.ClientID%>);

    pricePad.disabled = false;