繁体   English   中英

检索禁用 ASP.NET 文本框的值

[英]Retrieving value of disabled ASP.NET textbox

我有 3 个 ASP.NET 文本框和一个 HiddenField。 第三个文本框的值(禁用)取决于其他两个文本框的值。

公式是

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。

Liz,您是否可以将文本字段设置为只读(ReadOnly=True)而不是使其禁用=True? 原因是当文本字段被禁用时,不会在 POST 请求中与表单一起提交。 看到这个问题。

如果你想让它看起来好像被禁用了,我想你可以将 CssClass 应用于按钮。

我会使用 2 个选项之一

  1. 从其他输入服务器端再次执行计算
  2. 使用 javascript 在表单提交时启用 txtPricePad 字段,见下文

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

    pricePad.disabled = false;

暂无
暂无

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

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