简体   繁体   English

TextBox值未分配给asp.net中的变量

[英]TextBox value not assigning to variable in asp.net

Java Script Java脚本

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 .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. 值显示在文本框上,但是当我单击保存按钮时, txtptotalamount正在获取null值。如果我将readonly="false"放置,它的工作正常。

You want to be able to save the result from the "txtPtotalamout" but you don't want it to be editable. 您希望能够保存“txtPtotalamout”的结果,但您不希望它是可编辑的。

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. 要显示它,并在javascript中更新该DIV的内容和隐藏字段。

Then you could display the total amount in that DIV when you load the form (and populate the hidden field). 然后,您可以在加载表单时显示该DIV中的总金额(并填充隐藏字段)。 You could even format the DIV to look like a text box if you wanted. 如果需要,您甚至可以将DIV格式化为文本框。

From http://codecorner.galanter.net/2009/10/09/postback-disabled-textbox/ 来自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. 假设您在ASP.NET应用程序中将TextBox控件的属性ReadOnly设置为True(或Enabled为False)以防止用户直接输入数据。 But you still want to update that text box's value via client-side JavaScript. 但是您仍然希望通过客户端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. 诀窍是 - 保持ReadOnly = FalseEnabled = True并模拟他们的行为。 Add following line of to your server-side code: 在服务器端代码中添加以下行:

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

where TextBox1 is your textbox control. TextBox1是你的文本框控件。 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. 为了进一步起作用,您可以将texbox的背景设置为“LightGray”,使其显示为禁用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM