我有 3 个 ASP.NET 文本框和一个隐藏字段。 第三个文本框(已禁用)的值取决于其他两个文本框的值。
公式为
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)而不是使其Disabled=True?原因是当文本字段被禁用时,不会随 POST 请求中的表单一起提交。 See this question.
如果你想让它看起来好像被禁用了,我想你可以将 CssClass 应用于按钮。
关于asp.net - 检索禁用的 ASP.NET 文本框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7005217/