3

Im trying to set an array in Javascript, and then give it values from the DataTable in the code behind like that :

for (var i = 0; i < '<%=dt_questionVideo.Rows.Count - 1 %>'; i++) {
        videoQarray[i] = '<%=Convert.ToInt32(dt_questionVideo.Rows['+i+'][0]) %>';
    }

Im getting an error

Too many characters in character literal

how can I manuover this one ?

6
  • Maybe videoQarray[i] = '<%=Convert.ToInt32(dt_questionVideo.Rows[i][0]) %>'; Commented Jul 24, 2012 at 14:05
  • Perhaps build the entire array serverside instead? Commented Jul 24, 2012 at 14:06
  • @JamesKyburz if I try this one, it says that The name 'i' does not exist in the current context Commented Jul 24, 2012 at 14:07
  • @geekchic thanks, but I have to make it on client side Commented Jul 24, 2012 at 14:08
  • Ok, well I don't think that method will work. I believe the server tags are executed before the javascript. Any particular reason it needs to be client side? Commented Jul 24, 2012 at 14:08

1 Answer 1

5

You can't really do it that way. If you need a javascript array like that, you're going to have to do something like this in the code behind:

        int[] videoQarray = new int[dt_questionVideo.Rows.Count - 1];
        for (var i = 0; i < dt_questionVideo.Rows.Count - 1; i++) {
            videoQarray[i] = Convert.ToInt32(dt_questionVideo.Rows[i][0]);
        }

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

        Page.ClientScript.RegisterStartupScript(this.GetType(), "registerVideoQArray", createArrayScript, true);
Sign up to request clarification or add additional context in comments.

2 Comments

and then I can use the "registerVideoQArray" as my client array ?
no, 'videoQarray' will be your array. "registerVideoQArray" is just a unique name provided to the script manager.

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.