I have the following aspx.cs :
public partial class BarChart
{
public class LabelsDetail
{
public string LabelId { get;set; }
public string LabelDesc { get; set; }
}
public List<LabelsDetail> LabelsDetails { get; set; }
public void InsertDataToLabelsDetails()
{
// Data comes from somewhere into "LabelsDetails"
}
}
and the following JS code in the ASPX page :
function setupBarChart(JQObjectContainer, JsonData) {
var hashTableSize = <%=this.LabelsDetails.Count%>;
var hashtable = {};
if (hashTableSize != 'undefined' && hashTableSize > 0)
{
for (var item in <%=this.LabelsDetails%>)
{
hashtable[item.LabelId] = item.LabelDesc;
}
}
}
How can I do a foreach on a server side list in the client side ?
At the moment I get Uncaught SyntaxError: Unterminated template literal
When I try to loop on the server side list (this.LabelsDetails) .
Thanks
this.LabelsDetailsis just going to turn into the typename, as if you had called.ToString()on it. You need to convert it to JSON, embed it in your JavaScript, convert from JSON back to a JavaScript array, and loop over that. Or even better, retrieve the list via AJAX calls to Web API.