Getting the value of a disabled ASP.NET text field

I have 3 ASP.NET text fields and one HiddenField. The value of the third text field (this is disabled) depends on the values ​​of the other two.

Formula

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

Assuming that the initial value of txtPricePad is 0 and txtCarton is 12. When txtPrice is changed to 1200, GetPricePerPad () will be called, so txtPricePad will be 100.

Javascript successfully changed the txtPricePad value to 100, but when I call txtPricePad from the code, its value is 0. That's why I assigned the result of the HiddenField formula as well. Are there any other ways to do this? I do not want to use HiddenField again.

+4
source share
2 answers

Liz, is it possible to make the text field readonly (ReadOnly = True) and not disable = True? The reason is that when the text field is disabled, it does not appear with the form in the POST request. See this question.

If you want it to look like it was disabled, I think you can apply CssClass to the button.

+10
source

I would use one of two options

  • Recalculate server-side from other inputs or
  • Use javascript to include the txtPricePad field in the submit form, see below

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

    pricePad.disabled = false;

+2
source

All Articles