TextBox.Text changed with js don't keep value on postback

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2007
Posts: 57
Reputation: culebrin is an unknown quantity at this point 
Solved Threads: 1
culebrin culebrin is offline Offline
Junior Poster in Training

TextBox.Text changed with js don't keep value on postback

 
0
  #1
Aug 6th, 2009
Hi,

I have a website on NET 2.0, there I have a textbox and after I change the text on it with a js function, try to save on the DB (I have to click on a imagebutton to save it), but on the postback the textbox.text property has the previous value.

I need some guidance on how to resolve this.

Here's my code:

JS inserted with a RegisterClientScriptBlock method: <-- This works fine
  1. <script type="text/JavaScript">
  2. function Calcular() {
  3. if (!(isNaN(parseFloat(document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagSaldoPrevioPago').value)) && isNaN(parseFloat(document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagPagoActual').value)))) {
  4. document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcSaldoPostPago').value = parseFloat(document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagSaldoPrevioPago').value) - parseFloat(document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagPagoActual').value);
  5. } else {
  6. document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcSaldoPostPago').value = '0.0';}
  7. }
  8. function ActualizaMonto() {
  9. if (!(isNaN(parseFloat(document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagSaldoPrevioPago').value)) && isNaN(parseFloat(document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagPagoActual').value)))) {
  10. document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagSaldoPrevioPago').value = parseFloat(document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagMontoTotal').value);
  11. } else {
  12. document.getElementById('ctl00_cphVentanillaAtencionMaster_txtdcPagMontoTotal').value = '0.0';}
  13. }
  14. </script>

on PostBack I try to do this:

oPagosBE.btPagActivo = True

oPagosBE.dcPagMontoTotal = Me.txtdcPagMontoTotal.Text
oPagosBE.dcPagPagoActual = Me.txtdcPagPagoActual.Text
oPagosBE.dcPagSaldoPrevioPago = Me.txtdcPagSaldoPrevioPago.Text   <--- THIS VALUE POSTBACKS WITH THE INITIAL VALUE (BEFORE JS)
oPagosBE.dtPagFechaRegistro = IIf(IsDate(Me.txtdtPagFechaRegistro.Text.Trim), Me.txtdtPagFechaRegistro.Text.Trim, #1/1/1900#)
oPagosBE.vcPagUsuarioRegistrante = Session.Item("vcUsuCodigo")
          
bExito = bPagosBL.Insertar(oPagosBE)

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 22
Reputation: ema005 is an unknown quantity at this point 
Solved Threads: 4
ema005 ema005 is offline Offline
Newbie Poster

Re: TextBox.Text changed with js don't keep value on postback

 
0
  #2
Aug 7th, 2009
I once had a similar challenge. The TextBox is not aware of the change you made because it is a server control and the change was not made on the server but on the client side.I Corrected the situation by changing my control to an html control. I hope this help.
Emmanuel Balogun
Senior .Net Developer
Precise Financial Systems
Lagos-Nigeria.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 15
Reputation: ebookfinder is an unknown quantity at this point 
Solved Threads: 1
ebookfinder ebookfinder is offline Offline
Newbie Poster

Re: TextBox.Text changed with js don't keep value on postback

 
0
  #3
Aug 9th, 2009
It can be done,

Please use this form,

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <title>Untitled Page</title>
  4. <script type="text/javascript">
  5. function setval() {
  6. themsg = document.getElementById("<%=txtMessage.ClientID%>");
  7. themsg.value="Hello World!";
  8. }
  9. </script>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <asp:TextBox ID="txtMessage" runat="server" Text=""></asp:TextBox>
  14. <asp:Button ID="btnSeeMessage" runat="server" Text="see" onclick="SeeMessage" />
  15. </form>
  16. <input id="cID" onclick="setval();" type="button" value="Set from client" />
  17. </body>
  18. </html>
From server side,
  1. protected void SeeMessage(object sender, EventArgs e)
  2. {
  3. Response.Write(txtMessage.Text);
  4. }

Please use <%=ServerControlName.ClientID%> everytime you wanna access a server control from client side.
Last edited by ebookfinder; Aug 9th, 2009 at 3:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 57
Reputation: culebrin is an unknown quantity at this point 
Solved Threads: 1
culebrin culebrin is offline Offline
Junior Poster in Training

Re: TextBox.Text changed with js don't keep value on postback

 
0
  #4
Aug 10th, 2009
I used the "Control.ClientID" to reference my control, but no luck...

and I can't use a client side control, because I need to access it in the server also...

I gave up, I know there's a solution out there, but I have no time... I'm on a schedule... Thanks a lot!! to all of you... !!


Originally Posted by ebookfinder View Post
It can be done,

Please use this form,

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <title>Untitled Page</title>
  4. <script type="text/javascript">
  5. function setval() {
  6. themsg = document.getElementById("<%=txtMessage.ClientID%>");
  7. themsg.value="Hello World!";
  8. }
  9. </script>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <asp:TextBox ID="txtMessage" runat="server" Text=""></asp:TextBox>
  14. <asp:Button ID="btnSeeMessage" runat="server" Text="see" onclick="SeeMessage" />
  15. </form>
  16. <input id="cID" onclick="setval();" type="button" value="Set from client" />
  17. </body>
  18. </html>
From server side,
  1. protected void SeeMessage(object sender, EventArgs e)
  2. {
  3. Response.Write(txtMessage.Text);
  4. }

Please use <%=ServerControlName.ClientID%> everytime you wanna access a server control from client side.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC