Javascript changes not saved

G

Guest

I'm using Javascript to change a value in an asp:textbox control on an
asp.net page, then submitting the page and persisting the data using c# code.
I can see the value in the textbox change when the Javascript runs, but when
the page is submitted, the c# event code still sees the original value in the
textbox, not the value that was updated by Javascript.


Here’s the javascript code that changes the value:

document.forms[0].txt1.value = calcCigScore();
alert (document.forms[0].txt1.value);

The alert shows me that the value has changed (I can also see it change on
the page).


And here’s the c# code from the SaveRecord event on the aspx page:

mIntake.CigScore = int.Parse(txt1.Text);

When tracing through the code, I see that txt1.Text still equals the value
that was originally loaded into the page.

Is there a reason why .Net doesn’t see the updated value? Anything I can do
to change that?

Thanks
 
M

Mark Rae

Is there a reason why .Net doesn't see the updated value? Anything I can
do
to change that?

I'm willing to bet that you're loading the original value of the record in
your Page_Load method, and have forgotten to wrap it in a if (!IsPostback)
loop...
 
G

Guest

It turns out that you get strange results if the textbox is set to disabled.
This is a calculated field and I have it disabled so the users can't modify
it. But eventhough I can see the value in the textbox change when I modify
it with Javascript, I still get the old value when I try to process the data
with c# code. It started working properly when I enabled the textbox.

Thanks for your response.
 
M

Mark Rae

It turns out that you get strange results if the textbox is set to
disabled.
This is a calculated field and I have it disabled so the users can't
modify
it. But eventhough I can see the value in the textbox change when I
modify
it with Javascript, I still get the old value when I try to process the
data
with c# code. It started working properly when I enabled the textbox.

Ah right - if you'd said that it was disabled, I'd have known straightaway!

This, ahem, feature of ASP.NET 2 is thankfully very easy to work round.

Don't do this:

<asp:TextBox ID="MyTextBox" runat="server" Enabled="false" />

Do this:

<asp:TextBox ID="MyTextBox" runat="server" />

And in the Page_Load of your code, do this:

MyTextBox.Attributes.Add("disabled", "true");

Same thing with readOnly...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top