Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating JavaScript variables from UpdatePanel

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 ;)

like image 578
Jacob Avatar asked Feb 23 '26 08:02

Jacob


2 Answers

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.

like image 160
Brian Mains Avatar answered Feb 25 '26 21:02

Brian Mains


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.

like image 44
James Johnson Avatar answered Feb 25 '26 22:02

James Johnson