Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to set value in javascript variable from code behing, and i am using this

var SelectedItemNumber = '<%=SelectedItemNumberCount%>';

in this code SelectedItemNumberCount is a public variable in code and it is increment from a private function, but it is not working and always SelectedItemNumber variable shows 0th item.

Please help.
Thanks
Posted
Comments
Schatak 9-Sep-15 2:55am    
you can use Hidden field for this.
F-ES Sitecore 9-Sep-15 5:04am    
It will have the value that SelectedItemNumberCount had when the page was initially rendered so use breakpoints to check the order the code runs in, see if the property is rendered before your value is incremented. Remember that your .net code isn't running inside the browser, the html source is simply a snapshot from the page was rendered.

1 solution

Use Hiddenfield and store the server variable value in it and get the value into the javascript.

On aspx
C#
<input type="hidden" id="myHiddenId" runat="server" />

In aspx.cs
C#
myHiddenId.Value = Severvariable

On Javascript
JavaScript
var JSVariableValue = document.getElementById('myHiddenId').value; 
 
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