简体   繁体   中英

Will aspx not see changes made by javascript?

I am changing the contents of a span with javascript. I can see that it has changed with view source.

When I try to get the contents in ac# function right after, it still has the old value.

My guess is that asp is just seeing the html that it sent the client, and that javascript is changing the client html of which asp cannnot see?

this was my only guess on how to explain it. Would this be correct?

Thanks.

Unless what you are changing support POSTing the values back to the server, then your changes will be discarded. <span> 's do not get POSTed to the server.

If you change the value of INPUT's however, then the server will be able to see those new values on the nest page submission POST, provided they are runat=server .

ASPX is SERVER SIDE . Javascript is CLIENT SIDE . The two do not mix unless you make a call back to a webservice, or something similar.

Your <span> change is only on the client, and the server is not aware of it. Look into AJAX as a solution if you wish to communicate interaction back to the server.

Yes, your guess is correct!

Javascript sees only the client side and ASP.NET only the server side. If you make some change on the client side and you want to server side to be aware of that change, you can make use of AJAX .

See Ajax for more info.

In .NET you can use a WebService, a Controller, or a Page for receiving the ajax call.

add runat="server" to your span. this will cause it to get picked up by viewstate and the new value will be available during postbacks.

edit: according to jrummel and testing by mikemanne, a span will not post back even when decorated with runat=server .

I would suggest using an input (as others suggest) instead of a span if you want the value consistently available on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM