<script language="javascript" type="text/javascript">
function Calcaulate()
    {
       
       if(document.getElementById("drpPhoneList").value=="Home Phone Lite")
       {
         var txtRegular= document.getElementById("txtRegularRs").value;
         document.getElementById("txtTotalRegularRs").value=parseFloat(txtRegular);
         document.getElementById("TotalRsTaxes").value=parseFloat(txtRegular)+parseFloat(3.63);
        
       } 
</script>

    <input type="button" value="CALCULATE" onclick="Calcaulate()" class="button" />
<asp:Button ID="btnAdd" runat="server" Text="ADD" OnClientClick="return Validations()"
                    OnClick="btnAdd_Click" class="button"/>

as i have two buttons one is html and other is aps.net button what i want to do is that first i want to make calculation on client side as shown in above code of calculate function of javascript after calculation i want to insert the result in to sqlserver db by clicking on the asp.net button but when post back occurs it can not maintain the result it just clears the textbox what should i do any trick for that

Dani AI

Generated

The value disappears on postback because the textbox was disabled. In HTML, disabled inputs are not submitted, so the server never sees the client-side change and the control re-renders without it. Your final fix of keeping the control enabled and making it read-only is the correct pattern, . An UpdatePanel will not change that behavior; it only avoids a full refresh. ’s session approach can work but is not required here, and ’s AJAX suggestion does not address the root cause.

To make this solid and repeatable: keep the TextBox enabled, mark it read-only, and run the calculation right before postback by wiring it to the ASP.NET button’s OnClientClick. Also set the value using the control’s client ID (or use ClientIDMode="Static" on .NET 4+). If you want a belt-and-suspenders approach, mirror the value into a HiddenField and read that on the server. If you already have OnClientClick="return Validations()", combine them: OnClientClick="calcTotal(); return Validations();" so the math runs before the postback.

<asp:TextBox ID="TotalRsTaxes" runat="server" ReadOnly="true" />
<asp:HiddenField ID="hfTotal" runat="server" />
<asp:Button ID="btnAdd" runat="server"
            OnClientClick="calcTotal(); return true;"
            OnClick="btnAdd_Click" />
function calcTotal() {
  // compute your total here
  var box = document.getElementById('<%= TotalRsTaxes.ClientID %>');
  var total = /* your math */;
  box.value = total;
  document.getElementById('<%= hfTotal.ClientID %>').value = total;
}

Recommended Answers

All 8 Replies

On the page_load event check you

Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
if TotalRsTaxes.text.length > 0 then
session("TotalRsTaxes") = TotalRsTaxes.text
end if
end sub

On Button click Retrive the same Values

TotalRsTaxes.text = session("TotalRsTaxes")

Mark as solved if it helps you!!!

thx for ur such reply yousf! i tried the code that u have written but it's not working still the same problem text box gets cleard when page is post back by clicking on serverside button control.i haven't worked with javascript b4 iam in my infancy with that plzzz help

Yes the textbox will always be clear. there is only one Solution to this , is to use the flickering of the page causes this, what you need to do, is to add a scriptmanager to your page and add an updatepanel and add the textbox inside the Update panel. This solution will only work for a asp.net Textbox , but if its a html textbox then after the calculation you need to reassign the session value to the textbox as reach_yousuf demostrated.use the alerts to check the contents of the session.

Yes the textbox will always be clear. there is only one Solution to this , is to use the flickering of the page causes this, what you need to do, is to add a scriptmanager to your page and add an updatepanel and add the textbox inside the Update panel. This solution will only work for a asp.net Textbox , but if its a html textbox then after the calculation you need to reassign the session value to the textbox as reach_yousuf demostrated.use the alerts to check the contents of the session.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                 <ContentTemplate>
                    <asp:TextBox ID="TotalRsTaxes" runat="server" Enabled="false" >0</asp:TextBox>
                 </ContentTemplate>
                </asp:UpdatePanel>

THX FOR UR POST AS U SAID I GUESS I DID THAT BUT STILL I AM FACING THE SAME PROBLEM TEXT BOX GETS CLEARD. OK NOW I AM GOING TO GIVE U AGAIN THE FULL DETAIL THAT HOW MY APPLICATION WORKS

I HAVE THREE TEXT BOXES TXT1,TXT2,TXT3 OF ASP.NET NOW THE VALUES IN TXT1 AND TXT2 COMES FROM DATA BASE NOW I HAVE BUTTON OF HTML WHERE I USE JAVASCRIPT CODE AS IN 1ST POST I HAVE GIVEN THE CODE A FUCNTION CALCULATION WILL BE EXECUTED AND AFTER THIS THE VALUE TXT1+TXT2 WILL BE ASSIGN TO TXT3 AND AFTER THE VALUE PLACED IN TXT3 NOW I HAVE ASP.NET BUTTON WHEN DOESN'T HAVE ANY CODE I JUST TRIED THIS BUTTON FOR THE SENSE OF POSTBACK I JUST WANT TO CHECK THAT BY POSTING BACK WEITHER THE VALUE REMAIN IN TXT3 OR NOT BUT WHEN I CLICK ON ASP.NET BUTTON AFTER POSTBACK TXT3 BECOME CLEAR NOW I HOPE THAT U HAVE GOOD DETAIL ABOUT MY WORK THAT WHAT I WANT TO DO THXXXXXXXX PLZZZZZZ HELP THIS ONE

Hi Guys

You can try Page_Prerender Event instead of Page load coz page_prerender fires and earlier than Page_load. Pls. try and let me know and dont forget to mark as solved if it helps you.

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

if TotalRsTaxes.text.length > 0 then
session("TotalRsTaxes") = TotalRsTaxes.text
end if

    End Sub

On button click the retrive the same

TotalRsTaxes.text = session("TotalRsTaxes")

Hi Guys

You can try Page_Prerender Event instead of Page load coz page_prerender fires and earlier than Page_load. Pls. try and let me know and dont forget to mark as solved if it helps you.

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

if TotalRsTaxes.text.length > 0 then
session("TotalRsTaxes") = TotalRsTaxes.text
end if

    End Sub

On button click the retrive the same

TotalRsTaxes.text = session("TotalRsTaxes")

still it doesnt work do u have hotmail ,yahoo id comone messenger we might have better discussion on that my ids are ,

Hi Guys

You can try Page_Prerender Event instead of Page load coz page_prerender fires and earlier than Page_load. Pls. try and let me know and dont forget to mark as solved if it helps you.

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

if TotalRsTaxes.text.length > 0 then
session("TotalRsTaxes") = TotalRsTaxes.text
end if

    End Sub

On button click the retrive the same

TotalRsTaxes.text = session("TotalRsTaxes")

ok mr yousf thx for your help i got a solution it was very simple i set text fields as enabled=false that's why values were cleard i set textboxes enable to true and in page load i have just code single line txt.attributes.add("readonly","readonly")

Yes the textbox will always be clear. there is only one Solution to this , is to use the flickering of the page causes this, what you need to do, is to add a scriptmanager to your page and add an updatepanel and add the textbox inside the Update panel. This solution will only work for a asp.net Textbox , but if its a html textbox then after the calculation you need to reassign the session value to the textbox as reach_yousuf demostrated.use the alerts to check the contents of the session.

ok thx for your help i got a solution it was very simple i set text fields as enabled=false that's why values were cleard i set textboxes enable to true and in page load i have just code single line txt.attributes.add("readonly","readonly")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.