简体   繁体   中英

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 these 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>

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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