0

I have a DataTable that I get from the DB, I want to create a 2d array in the code behind (once I get the DataTable..), and then pass it as a 2d array to Javascript.

this is what I tried to code :

int[,] videoQarray = new int[dt_questionVideo.Rows.Count,dt_questionVideo.Columns.Count ];
        string[,] videoQarrayTitle = new string[dt_questionVideo.Rows.Count, dt_questionVideo.Columns.Count ];

        for (var i = 0; i < dt_questionVideo.Rows.Count ; i++)
        {
            for (int j = 0; j < dt_questionVideo.Columns.Count; j++)
            {

                videoQarray[i,j] = Convert.ToInt32(dt_questionVideo.Rows[i][0]);
                videoQarrayTitle[i,j] = dt_questionVideo.Rows[i][1].ToString();   
            }
        }

        string createArrayScript = string.Format("var videQarray = [{0}];", string.Join(",", videoQarray));
        createArrayScript += string.Format("var videQarrayList = [{0}];", string.Join(",", videoQarrayTitle));

        Page.ClientScript.RegisterStartupScript(this.GetType(), "registerVideoQArray", createArrayScript, true);

well, in the browser console it says that videoQarray is not defined.. I wonder how can I do that properly..

2 Answers 2

1

Probably the variable is being defined inside a function and therefore is hidden for other parts of code. Try "window.videoQArray" insted of "var ":

string createArrayScript = string.Format("window.videQarray = [{0}];", string.Join(",", videoQarray));
createArrayScript += string.Format("window.videQarrayList = [{0}];", string.Join(",", videoQarrayTitle));

Edit: It's a 2d array (ok, you wrote that very clearly in the question but I didn't see). Use JavaScriptSerializer:

var serializer = new JavaScriptSerializer();
string createArrayScript = string.Format("window.videQarray = {0};", serializer.Serialize(videoQarray));
createArrayScript += string.Format("window.videQarrayList = {0};", serializer.Serialize(videoQarrayTitle));
Sign up to request clarification or add additional context in comments.

Comments

0

Use The following function:

 public static string ArrayToString2D(string[,] arr)
    {
        StringBuilder str = new StringBuilder();
        str.Append("[['");
        for (int k = 0; k < arr.GetLength(0); k++)
        {
            for (int l = 0; l < arr.GetLength(1); l++)
            {
                if (arr[k, l] == null)
                    str.Append("','");
                else
                    str.Append(arr[k, l].ToString() + "','");
            }
            str.Remove(str.Length - 2, 2);
            str.Append("],['");
        }
        str.Remove(str.Length - 4, 4);
        str.Append("]]");
        return str.ToString();
    }

in the code behind have the following properties:

private string[,] upperLabels ;
    public string UpperLabel
    {
        get
        { return Utils.ArrayToString2D(upperLabels); }
    }

in the javascript use the following :

var upperSplitted = <%=UpperLabel%> ;
var value = upperSplitted[0][0];

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.