简体   繁体   English

ASP.NET:回发后如何保留使用javascript在服务器控件上设置的值

[英]ASP.NET: How to retain values set on server controls with javascript after post back

I'm creating an ASP.NET website which will does some calculations (if it's relevant it uses some formulas to calculate retirement plans) the first page, has a bunch of fields and labels that the user will input the numbers, and on key-down i have the JavaScript code running calculations and updating the labels with the numbers. 我正在创建一个ASP.NET网站,该网站的第一页将进行一些计算(如果相关的话,它会使用一些公式来计算退休计划),并包含一堆字段和标签,用户可以在其中输入数字,并在键盘上输入-下来,我让JavaScript代码运行计算并使用数字更新标签。 Then I want to pass these values over to the next page where they will be parsed and put into a bar graph for a summary, However I keep getting the default value of the labels. 然后,我想将这些值传递到下一页,在该页面将对其进行解析并放入条形图中以进行摘要,但是我一直在获取标签的默认值。 I know this is related to the postback of the page. 我知道这与页面的回发有关。

I have tried using hidden fields, to store the values but they also are being reset. 我尝试使用隐藏字段来存储值,但它们也正在被重置。 A lot of the solutions I've seen that are similar to that here I've also tried to no avail. 我见过的许多解决方案与这里的解决方案类似,但我也都尝试不了。

The javascript code is as follows: javascript代码如下:

function RATotal() {
    $('#lblRATotal').text("$" + (masterRANQAptERI + masterRAPTQptERI + 
                                 masterRARQAptERI + lumpsum).format());

    masterRATotal = (masterRANQAptERI + masterRAPTQptERI + masterRARQAptERI + lumpsum);

    document.getElementById('<%= hiddenRAtotal.ClientID %>').value = masterRATotal;
    document.getElementById('hideRAtotal').value = masterRATotal;
}

the ASP/HTML: ASP / HTML:

<tr>
  <td>
    <span>Total After-Tax Equivalent Contribution: </span>
    <asp:Label runat="server" ID="lblRAATTotal" Text="$X.XX" Font-Bold="true" />
  </td>
  <td>
    <span>Total Pre-Tax Equivalent Contribution: 
      <asp:Label runat="server" ID="lblRAPTTotal" Text="$X.XX" Font-Bold="true" />
    </span>
  </td>
  <td>
    <span>Total Retirement Retirement Income:</span>
    <br />
    <asp:Label runat="server" ID="lblRATotal" Text="0.00" Font-Bold="true" />
    <asp:HiddenField runat="server" ID="hiddenRAtotal" EnableViewState="True" />
    <input id='hideRAtotal' name='hideRAtotal' type='hidden' />
  </td>
</tr>

The C# code is as follows C#代码如下

protected void btnSummary_Click(object sender, EventArgs e)
{
    string RAOtotal2 = hiddenRAtotal.Value.ToString();
    Session["RAOtotal"] = RAOtotal2
    Response.Redirect("ChartDataQuery.cshtml");
}

Based on everything I've read, this should do the trick... but I am actually just getting a "," or null when I try to play around with it. 根据我读过的所有内容,这应该可以解决问题……但是当我尝试使用它时,我实际上只是得到一个“或”或null。 I've deducted the "," isn't a miscalc because when I set the value to a manually defined string i would also just get a ",". 我扣除了“,”并不是错误的计算,因为当我将值设置为手动定义的字符串时,我也会得到“,”。 I am still learning these kinds of things, and I would appreciate any help! 我仍在学习这些东西,我将不胜感激! Thanks! 谢谢!

Thanks to @wazz for helping me out with this one. 感谢@wazz帮助我解决了这一问题。

the line, document.getElementById('<%= hiddenRAtotal.ClientID %>').value = masterRATotal; 该行document.getElementById('<%= hiddenRAtotal.ClientID %>').value = masterRATotal; is what was breaking this. 是什么打破了这个。 I removed the line, and it is now working. 我删除了该行,现在可以正常工作了。

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

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