DueFrom
asked on
Calculate Sum on aspx page, then insert figure on aspx.vb page
function fnCalculateSum()
{
var result = 0;
if(document.getElementById ("<%=Textb ox1.Client ID%>"))
{
result = result + parseInt(document.getEleme ntById("<% =Textbox1. ClientID%> ").value);
}
if(document.getElementById ("<%=Textb ox2.Client ID%>"))
{
result = result + parseInt(document.getEleme ntById("<% =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.
{
var result = 0;
if(document.getElementById
{
result = result + parseInt(document.getEleme
}
if(document.getElementById
{
result = result + parseInt(document.getEleme
}
document.getElementById("<
}
</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.
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
__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.GetPostBackEventRe ference(yo urControlI D)%>");
__doPostBack(' <%=Page.GetPostBackEventRe
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.
<asp:TextBox ID="TextBox1" runat="server" onChange="fnCalculateSum()
<asp:TextBox ID="TextBox2" runat="server" onChange="fnCalculateSum()
<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
your javascript should look like this
of course you will need to change the SendToServer.UniqueID in the javascript function to match your button id
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" />
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>
of course you will need to change the SendToServer.UniqueID in the javascript function to match your button id
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="tru e"/> in configuration or <%@ Page EnableEventValidation="tru e" %> 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.Regist erForEvent Validation method in order to register the postback or callback data for validation.
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="tru
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" />
add a script manager in your master page just after the form
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="sc" />
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.getEle mentById(' contentDiv ').innerHT ML;
}
</script>
So it looks like I should scrap all this and look into AJAX to do this?
A page can have only one server-side Form tag.
I have this in my master page:
function setHiddenVal()
{
document.getElementById('<
}
</script>
So it looks like I should scrap all this and look into AJAX to do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much for all your help.
You welcome
in your case...best solution is to force a postback...this way your code behind file will get the updated value from textbox.