Javscript wont show ASP.NET var current value

C

Cirene

I have javascript code that does an alert on a public asp.net var...
alert("<%=strMessage%>");
The code runs whenever my ajax related code completes.

I declared the public var like this...
Public strMessage as string = "PUBLIC MESSAGE"

In my code I changed the variable but when the alert runs it always has the
original value, even though the var has been updated.

Any ideas why?
 
N

Nathan Sokalski

Well, the first thing I would do (if you haven't already) is do a View
Source to see if what you expected is what is really being sent to the
browser. Also, and this is just a personal preference, I do not completely
trust using the <%= %> tags, so you may want to try using the methods of
Page.ClientScript instead. At what point do you change the value of
strMessage? One of the situations that has always been a real pain for me
when debugging in many cases is when the problem is caused by when I do
something. Hopefully some of this will help, good luck!
 
C

Cirene

Thanks for teh info. Instead of using a public var I tried using a label on
teh form so that I could see the value.

I tried to 'alert' the value of teh label but it's not showing until after
teh js runs (then the label is set).

Basically here's all my js...
<script type="text/javascript">
//add event handlers to the search UpdatePanel
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(startRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);

function startRequest(sender, e) {
//disable controls during the AJAX call
document.getElementById("<%=Panel1.ClientID%>").disabled = true;
}

function endRequest(sender, e) {
//re-enable controls once the AJAX call has completed
document.getElementById("<%=Panel1.ClientID%>").disabled =
false;
if
(document.getElementById("<%=lblPublicMessage.ClientID%>").text != "")
alert(document.getElementById("<%=lblPublicMessage.ClientID%>").text);
}
</script>

This is an ajaxified webform. When I click my 'update db' button it runs
for a while (hence the js code to disable all the controls on the webform.)
In the vb.net code I set lblPublicMessage with a status. I would think it
would be between StartRequest and EndRequest.

When I tried to read lblPublicMessage it's either undefined or ''. After
the alert the label is updated.
 
J

Juan T. Llibre

re:
!> In my code I changed the variable but when the alert runs it always
!> has the original value, even though the var has been updated.
!> Any ideas why?

Unless you postback the page, the Javascript variable can't be updated at the server.
 
C

Cirene

So in an ajaxified page how can I do this? I do see that the label changes
by the vb.net code, but it's after the javascript code runs...
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top