The server code block is evaluated with the rendering of the page. If you are using AJAX and you change the server variable this will not affect the script if it is not included in the AJAX request. What I'd suggest you do is to register a script from the code-behind and populate some global variable with the value you need (e.g. the ScriptManager.RegisterStartupScript() method). Or, simply use a hidden field.