Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving value of disabled ASP.NET textbox

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

The formula is

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 the value of txtPrice is changed to 1200, GetPricePerPad() will be called, thus txtPricePad will be 100.

Javascript successfully changed the txtPricePad's value to 100 but when I am calling txtPricePad from the codebehind, its value is still 0. That's why I assigned also the result of the formula to a HiddenField. Are there other ways to do this? I do not want to use HiddenField again.

like image 456
Liz Avatar asked Aug 10 '11 02:08

Liz


People also ask

How to get value from disabled textbox in c#?

what you can do is: add a hidden field, when disable the textbox, set its value to the hidden field. then when you need the textbox value, check whether it is NullOrEmpty first. If no, yes textbox's value. otherwise, use the hidden field's value.

How to get value of disabled textbox in JavaScript?

Get value of Disabled TextBox value set using JavaScript on Server side in ASP.Net. I want to assign value to text box through Java Script and then access textbox value in C# code. Input fields marked with disabled="disabled" never send their value to the server when the form is posted. Make use of ReadOnly attribute.


2 Answers

Liz, is it possible for you to make the text field readonly (ReadOnly=True) as opposed to making it Disabled=True? The reason being that when a text field is disabled is not submitted with the form in the POST request. See this question.

If you want to make it look as if it was disabled, I guess you can apply a CssClass to the button.

like image 155
Icarus Avatar answered Nov 14 '22 21:11

Icarus


I would use one of 2 options

  1. Perform the calculation again server side from the other inputs or
  2. Use javascript to enable the txtPricePad field on form submit, see below

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

    pricePad.disabled = false;

like image 24
Jon P Avatar answered Nov 14 '22 22:11

Jon P