【问题标题】:Retrieving value of disabled ASP.NET textbox检索禁用的 ASP.NET 文本框的值
【发布时间】:2011-08-10 02:24:25
【问题描述】:

我有 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。

【问题讨论】:

标签: asp.net textbox


【解决方案1】:

Liz,您是否可以将文本字段设为只读 (ReadOnly=True) 而不是将其设为 Disabled=True?原因是当文本字段被禁用时,不会在 POST 请求中与表单一起提交。 See this question.

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

【讨论】:

  • 我知道这是一年前的事了,但试图检索禁用文本框的值浪费了整整 8 小时 - 你的回答奏效了
【解决方案2】:

我会使用 2 个选项之一

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

    var pricePad = document.getElementById();

    pricePad.disabled = false;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    相关资源
    最近更新 更多