How to maintain JavaScript values during PostBack in ASP.Net?
I have written one JavaScript to Calculate the TotalWeight based on two TextBox integer values. I multiplied thes开发者_如何学运维e two values and displayed in the 3rd TextBox Using JavaScript. But the problem is, I have one radiobuttonlist, in its selectedindexchanged event, the value I got in the 3rd TextBox gets disappeared. How to solve this?
My JavaScript is
<script type="text/javascript">
function TotalWeight()
{
var D1 = document.getElementById('<%=txtD1.ClientID%>');
var SectionWgt = document.getElementById('<%=txtSectionWeight.ClientID%>');
var TotalWgt = 0*1;
TotalWgt = parseFloat(D1.value) * parseFloat(SectionWgt.value);
if(isNaN(TotalWgt))
document.getElementById('<%=txtTotalWgt.ClientID%>').innerText = "0.000";
else
document.getElementById('<%=txtTotalWgt.ClientID%>').innerText = TotalWgt.toFixed(3);
}
</script>
<asp:TextBox ID="txtD1" runat="server" Width="136px" onkeyup="return TotalWeight();"></asp:TextBox>
After you calculate and assign the value to your Total Textbox, then put that value in a hidden field
as well, and then on postback reassign that value to the textbox from the hidden Field
.
Read the value of the textbox in when the radio button event fires and posts back - and then write it back to the textbox afterwards.
I would imagine that when the event fires and the page posts back, the third text box reverts to its original value - capture the new value and write it back to the box during the event.
use hidden field to store values. just use
[intput type="hidden" id="someid">]
after that you can use the value using $("#someid").val() Try, this helped me.
精彩评论