Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
>i have

@
{
string Map = string.Empty;

}


var nonID = 1;
@Map=nonID
</script

<b>What I have tried:</b>

i have tried

<pre>i have

@
{
string Map = string.Empty;

}

&lt;script>
var nonID = 1;
@Map=nonID
&lt;/script</pre>

but not working
Posted
Updated 13-Jul-17 5:14am
Comments
F-ES Sitecore 13-Jul-17 8:44am    
You have to remember that your .net code runs first (bits in "{ }" and after "@") which generates html that is then sent to the browser. So your js only exists and is executed after the server code runs, so you can't initialise a server-side variable based on javascript and the javascript doesn't execute until after the .net code has finished.

1 solution

As F-ES Sitecore wrote, the JavaScript code is run (in the client side when the browser shows the HTML document) only after the .NET code has been run (in the server side) and, an HTML document has been created from it. The JavaScript code isn't run when rendering the page in the server side (it just a text that is copied to the created HTML document). So it can't work by just writing a simple assignment (like you tried).


But, if you want a mechanism for changing a server's .NET objects values according to client's JavaScript objects values, you can try my WebBinding.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900