Javscript wont show ASP.NET var current value

Discussion in 'ASP .Net' started by Cirene, Aug 17, 2008.

  1. Cirene

    Cirene Guest

    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?
     
    Cirene, Aug 17, 2008
    #1
    1. Advertising

  2. 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.nathansokalski.com/

    "Cirene" <> wrote in message
    news:...
    >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?
    >
     
    Nathan Sokalski, Aug 17, 2008
    #2
    1. Advertising

  3. Cirene

    Cirene Guest

    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.

    "Nathan Sokalski" <> wrote in message
    news:...
    > 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.nathansokalski.com/
    >
    > "Cirene" <> wrote in message
    > news:...
    >>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?
    >>

    >
    >
     
    Cirene, Aug 17, 2008
    #3
  4. 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" <> wrote in message news:...
    >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?
    >
     
    Juan T. Llibre, Aug 17, 2008
    #4
  5. Cirene

    Cirene Guest

    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" <> wrote in message
    news:%23YprT$...
    > 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" <> wrote in message
    > news:...
    >>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?
    >>

    >
    >
     
    Cirene, Aug 17, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    365
    rooster575
    Dec 2, 2003
  2. Darrel
    Replies:
    4
    Views:
    2,270
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Oct 26, 2004
  3. thomson
    Replies:
    10
    Views:
    2,498
    Eliyahu Goldin
    Jun 20, 2005
  4. Benton
    Replies:
    4
    Views:
    1,123
    Mr Newbie
    Nov 26, 2005
  5. Replies:
    1
    Views:
    292
    Thomas 'PointedEars' Lahn
    Mar 19, 2008
Loading...

Share This Page