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 ;)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With