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

DueFrom
DueFrom used Ask the Experts™
on
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.  

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Saqib KhanSenior Developer

Commented:
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)%>");
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
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

Author

Commented:
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" />

Author

Commented:
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?
Ok you definitely have something strange going on.
in ASP.NET only a single form should have runat server. i seen you are using master pages but how many forms do you have on all of your site? I tested with one form like all ASP.NET site should have and I have gotten the expected results OK if you want to go the ajax way, use an update panel and solve to avoid refreshing the whole page up on postback.

Author

Commented:
Thank you so much for all your help.
You welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial