Link to home
Start Free TrialLog in
Avatar of DueFrom
DueFrom

asked on

Calculate Sum on aspx page, then insert figure on aspx.vb page

function fnCalculateSum()
{
var result = 0;
if(document.getElementById("<%=Textbox1.ClientID%>"))
{
result = result + parseInt(document.getElementById("<%=Textbox1.ClientID%>").value);
}
if(document.getElementById("<%=Textbox2.ClientID%>"))
{
result = result + parseInt(document.getElementById("<%=Textbox2.ClientID%>").value);

}
document.getElementById("<%=Textbox9.ClientID%>").value = result;

}
</script>

I am using above code in my aspx page to sum textbox1 and textbox2 and show the calculated sum in textbox9.  But on aspx.vb page, if i try to do a insert into sqldatabase the TextBox9.Text  is empty here.   I must be missing something that brings the value from 1st page into code behind.  

Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

you cant do this....client side CANNOT be combined with Server Side...unless you are usng AJAX.

in your case...best solution is to force a postback...this way your code behind file will get the updated value from textbox.
You can force the postback event with something like this

__doPostBack('<%= yourControl.UniqueID %>', '');

where yourControl is the ID of the button you are clicking to do the calculation.

This is based on an assumption since you havent posted any of the aspx markeup
better option is to use
__doPostBack(' <%=Page.GetPostBackEventReference(yourControlID)%>");
Avatar of DueFrom
DueFrom

ASKER

So I have these textboxes on aspx side:
<asp:TextBox ID="TextBox1" runat="server" onChange="fnCalculateSum();" TabIndex="1"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"  onChange="fnCalculateSum();"  TabIndex="2"></asp:TextBox>
<asp:TextBox ID="TextBox9" runat="server" BackColor="#CCCCCC" ReadOnly="True">
 
They call the javascript noted above - which shows the users the calc as they input figures.  but then on the aspx.vb page I want to be able to insert the textbox9 (sum of textboxes) value.   But textbox9.text  is empty.   I was trying to do all this without having the users click a calc button, thats why i used java script, but i definitely need that value when they hit submit button to insert the data into the database.
first of all you can not get any value from a ReadOnly control.
if you want a read only use a label control instead of the TextBox
 <asp:TextBox ID="TextBox1" runat="server" onChange="fnCalculateSum();" TabIndex="1"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"  onChange="fnCalculateSum();"  TabIndex="2"></asp:TextBox>
<asp:TextBox ID="TextBox9" runat="server" BackColor="#CCCCCC"/>
<asp:Button ID="SendToServer" Text="Calculate" runat="server" 
            onclick="SendToServer_Click" />

Open in new window



your javascript should look like this

 <script type="text/javascript">
        function fnCalculateSum() {
            var result = 0;
            if (document.getElementById("<%=TextBox1.ClientID%>")) {
                result = result + parseInt(document.getElementById("<%=TextBox1.ClientID%>").value);
            }
            if (document.getElementById("<%=TextBox2.ClientID%>")) {
                result = result + parseInt(document.getElementById("<%=TextBox2.ClientID%>").value);

            }
            if (result) {
                var frm = document.forms[0];
                document.getElementById("<%=TextBox9.ClientID%>").value = result;
               
                frm.__EVENTTARGET.value = '<%=SendToServer.UniqueID %>';
               frm.__EVENTARGUMENT.value = null;
                frm.submit(); 
              
            }

        }
</script> 

Open in new window


of course you will need to change the SendToServer.UniqueID in the javascript function to match your button id
Avatar of DueFrom

ASKER

Sorry, its probably easier if you see my entire code.   Just didn't want to clutter it up.   aspx.txt aspx-vb.txt


I don't think I followed your instructions correctly.  Now I get the below error:

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Note sure why you would that exception I tested the same code in VS.Net 2010 and VS.Net 2008 using framework 4 and 3.5.
add a script manager in your master page just after the form

 <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="sc" />
Avatar of DueFrom

ASKER

As soon as I added the ScriptManager i now get:
A page can have only one server-side Form tag.

I have this in my master page:  
     function setHiddenVal()
     {
        document.getElementById('<%= hidVal.ClientID %>').value=document.getElementById('contentDiv').innerHTML;
     }
    </script>

So it looks like I should scrap all this and look into AJAX to do this?
ASKER CERTIFIED SOLUTION
Avatar of Sammy
Sammy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DueFrom

ASKER

Thank you so much for all your help.
You welcome