Client side changes not reflected in page

G

Guest

Hi everyone!

I have a hidden input field in a form which I change in some occasions on
the client using javascript,
but when I use "view source" I can't see these changes reflected on the page!

Does anyone know what I should do in order for the page to know the change?
(Tried with enableviewstate=false, true, whatever, didn't work)

I would like to have the change reflected in the page without doing a
postback or on the postback!

Is this possible? Thanks in advance!
 
G

Guest

Stick this in the browser addressbar and you'll see the changes:
javascript:document.write("<xmp>"+document.documentElement.innerHTML+
"</xmp>");

Dynamic updates to the HTML DOM don't usually show up with "View Source".
Peter
 
B

bruce barker

view source shows what the server sent, not the current state of the
page. if javascript changed anything it is not reflected in the source.

for example view source of this page looks nothing like the rendered html:

<html>
<body>
<script>
for(var i=0; i< 100; ++i) document.write('hello ' + i + '<br>');
</script>
</body>
</html>


-- bruce (sqlwork.com)
 
G

Guest

Yes, but when the page is posted back to the server,
the change that javascript made isn't recognized by the server code,
while the server recognizes changes made through user input,
it doesn't know changes made by javascript! Why?
 
G

Guest

Yes, but when the page is posted back to the server,
the change that javascript made isn't recognized by the server code,
while the server recognizes changes made through user input,
it doesn't know changes made by javascript! Why?
 
T

Thomas Hansen

Stick this in the browser addressbar and you'll see the changes:
javascript:document.write("<xmp>"+document.documentElement.innerHTML+
"</xmp>");

[try to avoid "top posting"... ;) ]

Or you could use FireFox together with FireBug from which you have the
possibility to both INSPECT the DOM in addition to actually do CHANGES
to the DOM and CSS in addition to setting breakpoints in JavaScript
code...
Marvelous tool :)
I LOVE FireBug!!


..t
 
G

Guest

Well, ok, forget about the page's source,
my mistake mentioning this,
my real problem is that the variable show its 'original' value when
submitted to the server and not the one that javascript changed to!
Why isn't that variable/field 'refreshed'?
Do I need to do something (like a 'refresh')?
 
M

Mark Rae

Patrick,
Well, ok, forget about the page's source,
my mistake mentioning this,

It's a common misconception... :)
my real problem is that the variable show its 'original' value when
submitted to the server and not the one that javascript changed to!
Why isn't that variable/field 'refreshed'?
Do I need to do something (like a 'refresh')?

This, however, is a different issue...

Client-side data changes, whether caused by user typing or JavaScript,
should certainly survive a postback.

You'll need to post your code...
 
G

Guest

I've had the exact problem both in ASP.NET 1.1 and now I'm facing it in 2.0
as well!

Well, the case is this
(I'll mention the 1.1 case, which is easier to tell):

on a user input form (data entry - update a customer, let's say),
the user types the new data and presses the update button to commit the
changes,
however, javascript is called before the postback in order to validate the
input values,
if something goes wrong prompts the user to correct,
and, when everything is ok, posts the page to the server.

If I change a single value on a single field with javascript on this example
and then post back the page to the server, that change can't be seen by the
server,
while user values and changes of the fields can!

Why? How is that?
 
G

Guest

Hi Patrick,

I see the problem is that you don't quite understand how it works. Every
time you request a page from the browser, asp.net generates a HTML document
that is sent back and parsed by the browser. In addition to that browser
builds DOM objects based on parsed document so you could interact with
document on the client side via JavaScript / VBScript. That’s the bit you
seem to understand. Now (simplifying), if you want to send page back to the
server, only small part of the information is passed in POST request - a
special form is built to store values for <input> controls only (textbox,
radio button, button, select, etc.), so if you added or changed any property
via JavaScript (apart from input.value) it won’t be reflected in the form. In
other words, browser does not send changed HTML document back as you may
think, it sends input information only. Asp.net engine parses this
information and updates compiled page that’s literally a wrapper around the
HTTP request (community guys - please forgive me for simplifying, Op seems to
lack this knowledge). If you want to know the value of the client side
variable / property use hidden input to store it – it’ll be then available on
the server side, or use AJAX asynchronous calls (more advanced).

Hope everything is clear now

Regards

Milosz
 
M

Mark Rae

radio button, button, select, etc.), so if you added or changed any
property
via JavaScript (apart from input.value) it won't be reflected in the form.

Yes but, unless I've completely misunderstood the OP, he's not talking about
client-side JavaScript changing properties of DOM element e.g. backcolor,
borderwidth etc...

I believe he's talking about changing the *value* of input controls via
JavaScript, and this most certainly *should* survive the postback...
 
G

Guest

Yes Mark, in this we need to see the code. I suspect he changes soemthing else.

Patrick show us the code ! :)
 
G

Guest

hi there,

This is not longer a problem in asp.net 2.0. There's a special property of
button called OnClientClick:

<asp:Button runat="server" ID="btn"
OnClientClick="SetSomeHiddenFields();return true;" Text="Post Me!"/>

Regards
 
G

Guest

Well, I found exactly where the problem lies:

When the control that changes value is a web control!

Try yourselves the following code:
page:
Code:
<%@ Page Language="VB" AutoEventWireup="false" 
CodeFile="test2.aspx.vb" Inherits="test2" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

      <asp:label ID="lblTotalQuantity" EnableViewState="true" runat="server" 
BackColor="ButtonFace" Width="50px"></asp:label>

      <input type="text" runat="server" id="txtTest" />
      <input type="button" 
onclick="javascript:document.all['lblTotalQuantity'].innerHTML = '111';" />


      <input type="button" onclick="javascript:document.all['txtTest'].value 
= '111';" />
            
      <input type="button" runat="server" id="btnTest" />
    </div>
    </form>
</body>
</html>

and code behind:
Code:
Partial Class test2
  Inherits System.Web.UI.Page

  Private Sub MessageMake(ByVal strMessage As String)

    'Creates a messagebox with a message passed in

    Response.Write(String.Format("<script 
language=javascript>alert('{0}')</script>", strMessage))

  End Sub

  Protected Sub btnTest_ServerClick(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles btnTest.ServerClick

    MessageMake(lblTotalQuantity.Text)

  End Sub
End Class

When I update that asp label control with javasript and try to get its value
after posting back, not only I don't get it (I get blank),
but loses its value and becomes blank as well!

Why?
Could someone please tell me why this is happening?

Should I use a span with runat="server"?

Thanks in advance!
 
G

Guest

the above result is the same, if I use a span with runat="server"!

So, how do I tell asp.net to remember those values set by javascript?
 
B

bruce barker

only <input>, <textarea> and <select> values are posted back by the
browser. use a hiden field (<input type'hidden">)

-- bruce (sqlwork.com)
 

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,015
Latest member
AmbrosePal

Latest Threads

Top