1

.aspx.cs:

List<SomeObject> items = whatever.getList(); 

Session["records"] = items;

If I access the Session in my Javascript file it gives me a string like

'System.Collections.Generic.List`1[SomeObject]'

.js function:

var records = '<%= Session["records"] %>';

How can I convert the session into an array? Thanks

1
  • 1
    you can make a ajax call and get array of session Commented Mar 20, 2014 at 11:48

2 Answers 2

1

You have to iterate through that array and print the right values:

var records = [];

<% 
    foreach(var item in (List<SomeObject>)Session["records"]) { 
%>

records.push('<%= item.PropertyName %>');

<% 
    }
%>

Now you got an array in your script with those values.

To get an array of objects like { Property1: "value1", Property2: "value2" }, that stands for the same structur of your C# object, for example, you have use reflection.

Sign up to request clarification or add additional context in comments.

2 Comments

Works fine, but how can I read the C# native code in my javascript file?
@user3441641 what you mean?
1

Try this... I am not checking

<script>
    var someSession = '<%= Session["SessionName"].ToString() %>';

 </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.