1

I have a WebForms application in wich I store array of strings in Session object and I need to get this array in javascript code. Maybe someone can provide any solution, how should i do this?

Here is my code:

    function loadAnswers() {
        var answers = '<%=Session("answers")%>';
    }

but is doesn't work and answerss variable contain simple string after assignment. ('System.String[]')

1
  • You are initializing answers as a string. Sort of like doing "var answers = 'some value';" you have to initialize and populate answers like any other array in javascript. "var answers = ['a1', 'a2', ...];" Commented Oct 30, 2014 at 15:25

2 Answers 2

1

If you serialize the answers object to JSON, then you should be able to access it programatically from JavaScript.

var answers = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Session("answers")) %>;

UPDATE: Below is a working example using four different types of data (string, number, collection, object). This illustrates how JSON serialized from the server can be used as a JavaScript object literal client-side.

Code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFormsTestApp
{
    public partial class _Default : Page
    {
        protected string Name = "Alice Student";
        protected decimal GPA = 3.84M;
        protected List<string> Classes = new List<string>() { "World History", "Algebra II", "English", "Phys Ed", "Latin I", "Home Economics" };
        protected School School = new School() { Name = "Jefferson High School", County = "Hamilton County", Ranking = 5 };
    }

    public class School
    {
        public string Name { get; set; }
        public string County { get; set; }
        public int Ranking { get; set; }
    }
}

ASPX:

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormsTestApp._Default" %>

<script type="text/javascript">
    var name = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Name) %>;
    var gpa = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(GPA) %>;
    var classes = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Classes) %>;
    var school = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(School) %>;
    alert(name + ' goes to ' + school.Name + ', has a ' + gpa + ' GPA, and takes ' + classes.length + ' classes.');
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! was helpful!
-1

(I assume u use VB) You can use a code like this:

function loadAnswers() {
   var answers = ['<%= String.Join("','", CType(Session("answers"), String()))%>'];
}

It actually joins your array into a string, that is formatted into JS array representation.

So for example if you have an array like this

Session("answers") = New String() {"aaa", "bbb", "ccc"}

It will prodice a line like this

var answers = ['aaa','bbb','ccc'];

Which will be a real JS array. If you need a simple string instead, you can use

var answers = '<%= String.Join(",", CType(Session("answers"), String()))%>';

which will produce

 var answers = 'aaa,bbb,ccc';

7 Comments

Have to make sure that the answers don't contain any '
@the_lotus yup, additional checks/conversions might be in order, at the very least escape the apostrophe \' in the resulting string
-1: New String() {"Mixed O'quotes ""succeeded"""}. Generating correct JS/HTML/XML with string concatenation is major pain - try not to do so.
@AlexeiLevenkov Um.. see my comment above?
@YuriyGalanter :) you expect way too much of visitors of SO...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.