Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Javscript wont show ASP.NET var current value

Reply
Thread Tools

Javscript wont show ASP.NET var current value

 
 
Cirene
Guest
Posts: n/a
 
      08-17-2008
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?


 
Reply With Quote
 
 
 
 
Nathan Sokalski
Guest
Posts: n/a
 
      08-17-2008
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!
--
Nathan Sokalski
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.nathansokalski.com/

"Cirene" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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?
>



 
Reply With Quote
 
 
 
 
Cirene
Guest
Posts: n/a
 
      08-17-2008
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%>").di sabled = true;
}

function endRequest(sender, e) {
//re-enable controls once the AJAX call has completed
document.getElementById("<%=Panel1.ClientID%>").di sabled =
false;
if
(document.getElementById("<%=lblPublicMessage.Clie ntID%>").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.

"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!
> --
> Nathan Sokalski
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
> "Cirene" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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?
>>

>
>



 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      08-17-2008
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/
======================================
"Cirene" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>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?
>



 
Reply With Quote
 
Cirene
Guest
Posts: n/a
 
      08-17-2008
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...

"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:%23YprT$(E-Mail Removed)...
> 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/
> ======================================
> "Cirene" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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?
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Possible to show an iframe's current location (current URL) liketofindoutwhy@gmail.com Javascript 1 03-19-2008 10:20 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 10 06-20-2005 01:02 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 0 06-20-2005 10:54 AM
Threads.. Session var lost, App var ok Alvin Bruney ASP .Net 1 12-02-2003 01:56 AM
does "struct_name var = { 0 }; " fill var with 0? Fred C++ 3 08-10-2003 09:44 AM



Advertisments