3

I'm trying to update global javascript variables from ASP.NET code. What I've tried to do is use an UpdatePanel like that:

<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <ContentTemplate>
    <script type="text/javascript">
        var global1= <%= this.Method(parameter) %>;
        var global2= <%= this.Method(parameter) %>;
    </script>
</ContentTemplate>
</asp:UpdatePanel>

The UpdatePanel has a trigger (wich is not shown on the code) that fires the update. I have also an endRequest method:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

function EndRequest(sender, args) {
    compute();
}

If I use software like 'Firebug' to inspect the code I can perfecly see how global variables are updated to his new value (when asyncpostback occurs). Unfortunately if I put an alert showing his values inside compute function they have the previous value.

Where's the mistake? Is it possible to update the variables this way from ASP.NET?

Thanks a lot ;)

Jacob
  • 1,886
  • 2
  • 25
  • 40

3 Answers3

4

Try doing this: Put 2 global variables outside of the update panel. Then, when the UpdatePanel posts back and its an async post, from code do:

ScriptManager.RegisterClientScriptBlock(..);

In that statement, do:

"global1 = '" + this.Method(parameter) + "';";
"global2 = '" + this.Method(parameter) + "';";

So essentially, you write out an update statement to the variable.

HTH.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
1

You can try something this:

var global1= "\"" + <%= this.Method(parameter) %> + "\""; 

Or possibly this:

var global = "'" + <%= this.Method(parameter) %> + "'";

I'm also not sure that you need to put it in the update panel, unless there's something else going on that I'm not seeing.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
1

I really want to thank you for participate in this thread.

I've finally decided to change my initial idea and perform the actions solely with Javascript/Jquery with PageMethods (ASP.NET):

http://msdn.microsoft.com/es-es/library/byxd99hx(v=vs.80).aspx http://stackoverflow.com/questions/563133/using-jquery-to-call-a-webmethod

I don't like they so much, because I see them as a 'local' solution with few possibilities of beign reused. But they return automatically JSON response wich I can use with $.ajax jquery function.

The feeling with the interface is better now.

Thanks again,

Jacob
  • 1,886
  • 2
  • 25
  • 40