943,712 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 6428
  • ASP.NET RSS
Aug 6th, 2009
0

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

Expand Post »
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
ASP.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
culebrin is offline Offline
62 posts
since Aug 2007
Aug 7th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
ema005 is offline Offline
24 posts
since Jan 2006
Aug 9th, 2009
0

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

It can be done,

Please use this form,

ASP.NET Syntax (Toggle Plain Text)
  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,
ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
ebookfinder is offline Offline
19 posts
since Jul 2007
Aug 10th, 2009
0

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

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... !!


It can be done,

Please use this form,

ASP.NET Syntax (Toggle Plain Text)
  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,
ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
culebrin is offline Offline
62 posts
since Aug 2007
Jun 9th, 2010
0
Re: TextBox.Text changed with js don't keep value on postback
Anyone with a solution? Working on this for 3 days and dont know what to do.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aliensXY is offline Offline
11 posts
since Jul 2009
Jun 11th, 2010
0
Re: TextBox.Text changed with js don't keep value on postback
Are you setting the initial value of the textbox during the page load?
Reputation Points: 12
Solved Threads: 6
Newbie Poster
mikev2 is offline Offline
20 posts
since Oct 2009
Jun 12th, 2010
0
Re: TextBox.Text changed with js don't keep value on postback
Textbox is a server control,it loses value changed from javascript on postback.if u want to save the value,u can use hidden control which you can access in both client as well as server side.Please mark as answer if this works.
Last edited by ja3_bhende; Jun 12th, 2010 at 7:38 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ja3_bhende is offline Offline
6 posts
since Feb 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Delay Download Function Call
Next Thread in ASP.NET Forum Timeline: aspx page won't start in web site solution in VS2008





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC