Javscript wont show ASP.NET var current value

  • Thread starter Thread starter Cirene
  • Start date Start date
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?
 
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!
 
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.
 
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.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
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...
 
Back
Top