1

Java Script

function outputtax() 
 { 
 var tamount = parseFloat(document.getElementById('<%=txtpsubtotal.ClientID%>').value);
 var cash = parseFloat(document.getElementById('<%=txtpdiscount.ClientID%>').value);

 if (isNaN(tamount) != true && isNaN(cash) != true && isNaN(tax) != true)
  {
    document.getElementById('<%=txtPtotalamout.ClientID%>').value =
       Math.round(parseFloat(document.getElementById('<%=txtpsubtotal.ClientID%>').value)
  - parseFloat(document.getElementById('<%=txtpdiscount.ClientID%>').value))              
      return false;
    }

  }

<asp:TextBox ID="txtPtotalamout" runat="server" ReadOnly="true">
                                          </asp:TextBox>

.CS

objsupplyPL.totalamount = Convert.ToDouble(txtPtotalamout.Text.ToString());

Value is displaying on the textbox but when i click save button txtptotalamount is getting null value.If I placed readonly="false" it's working fine.

9
  • Are you getting an error message or are you finding that txtPtotalamout.Text is null? Commented Sep 13, 2013 at 15:24
  • @rwalter I am getting error "Input string is not correct format". While debugging its seems null Commented Sep 13, 2013 at 15:25
  • Have you tried objsupplyPL.totalamount = Convert.ToDouble(txtPtotalamout.Text); (removing the ToString()) Commented Sep 13, 2013 at 15:28
  • @rwalter i tried but no use. Commented Sep 13, 2013 at 15:31
  • Try with removing ReadOnly property Commented Sep 13, 2013 at 15:36

2 Answers 2

2

From http://codecorner.galanter.net/2009/10/09/postback-disabled-textbox/

Let’s say in your ASP.NET application you set a TextBox control’s property ReadOnly to True (or Enabled to False) to prevent user from entering data directly. But you still want to update that text box’s value via client-side JavaScript. Which is happening, the value can be updated. But during postback to the server – surprise, surprise! – the new value doesn’t persist. This is due to security precaution – if the value wasn’t meant to be changed – the change is not allowed. But there is a way around this.

The trick is - to keep ReadOnly = False and Enabled = True and simulate their behavior. Add following line of to your server-side code:

TextBox1.Attributes["onclick"] = "this.blur();"

where TextBox1 is your textbox control. What this line does is adds client-side behavior to the textbox. As soon as user tries to click the textbox, focus immediately gets lost, preventing user from entering data, making the textbox essentially read-only. For further effect you can set the texbox’s background to something like “LightGray” making it appear disabled.

Sign up to request clarification or add additional context in comments.

Comments

2

You want to be able to save the result from the "txtPtotalamout" but you don't want it to be editable.

You could just use

<div id="PTotalAmount"><asp:Label id="PTotalAmount" runat="server" /></div>
<asp:HiddenField ID="hPTotalAmount" runat="server" />

To display it, and update the contents of that DIV and the hidden field in the javascript.

Then you could display the total amount in that DIV when you load the form (and populate the hidden field). You could even format the DIV to look like a text box if you wanted.

1 Comment

I changed this answer after rethinking it, since it isn't always pretty to disable a text box on the client side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.