Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

losing Javascript values at postback

Status
Not open for further replies.

codecomm

Programmer
Feb 14, 2007
121
US
Okay, I know this is probably b/c of my javascript ignorance, but @ postback, I lose my javascript created values. I'm assuming it's b/c the view state isn't updated, so when the page posts back, the textbox values just go back to what's set in the "Text" property at design time...which is 0. Do I have to register the client script or something?

I'm trying to do an onblur and a simple division with two fields/textboxes to update a third textbox to save server posts.

Any help/ideas are appreciated!
 
As the javascript is client side, the server doesn't know of it's existence unless you tell it. Depending on what you are doing, you could add the javascript values to a hidden input which will then be posted to the server as part of the form.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
All these textboxes are asp.net textboxes, and "Enable View State" is true.

For my onblur situation, I can use the OnTextChanged in codebehind, but that's a little slow, but if I change it to the javascript it's almost instant, but postback makes the target textbox value back to 0.

When I try my button click to do a division, it works, but on postback, the value is back to 0. I can use codebehind, but it's a little slower.

I am using an AJAX tabs control with update panels...don't know if that's really an issue there.
 
Are you trying to do math just on the client or do you really need a postback?
 
javascript variables arent part of the viewstate. They are client side script variables. To get them in the post back you have to stick them into either textbox or hidden form fields just prior to the page submitting.

You need a javascript function that fires just before submit (i think its onSubmit or something like that)

in that function, copy your variables to hidden form fields.

This is how I did it when I needed to pass JavaScript variables back to the client, and it worked great.
 
bigfoot / NeilTrain,

Thanks for the posts!

I'm trying to do two things with javascript to avoid postbacks:
1. populate a "ReadOnly" text box with another textbox's value onblur (OnTextChanged with codebehind) so that two are in synch, and the "driver" textbox is the "control value" for the read-only textbox.
2. On a buttom click, I'm trying to calculate a decimal value based off two textbox values.

I've tried to use the ASP.NET image button's "OnClientClick", and that didn't help either.

So, even though the values are in an ASP:TextBox, and EnableViewState is set to True, I'll still need to use Hidden fields?

What's interesting is that in my javascript function, I had to use the following full name for the textboxes since they were in an AJAX tab control:

<script type="text/javascript">
function Synch()
{
var x=document.getElementById("TabContainer1_TabPanel3_txtParent").value;
document.getElementById("TabContainer1_TabPanel3_txtChild").value=x;
}
 
I used a normal button to avoid the postback when I needed a clear function for a form I was doing.

<inpur id="xxx" and so on

I found the asp button always gave me a server trip.
But you could turn off CausesValidation on your button too and it won't make the trip, thereby holding the value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top