0

I'm using ASP.NET and have a string array and I want to pass this array to jQuery. How can I do that?

I've tried the following but it isn't working. I have logged my array, and it's full so the array is not the problem.

    <script>
        var imagesSrc = '<%=images%>';
        for (var i = 0; i < imagesSrc.length; i++) {
            document.getElementById("image" + ((i + 1).toString())).src = "images/" + imagesSrc[i] + ".jpg";
        }
    </script>

I have given my elements increasing numerical ID's so they are matched by the selector "image" + ((i + 1).toString())"

One last thing if i use this it works.

    document.getElementById("image" + ((i + 1).toString())).src = "images/" + "muppets" + ".jpg";
2
  • Are you getting a specific error? Commented Jul 14, 2014 at 14:56
  • None just broken link img Commented Jul 14, 2014 at 14:58

3 Answers 3

1

Simple use comma separated values in images server side variable. For example names of images you want to use is image1,image2...imageN then use

protected string images="image1,image2...imageN"; // c# code behind

and in javascript code simple use split function to split comma separated values, like

 <script>
        var imagesSrc = '<%=images%>'.split(",");
        for (var i = 0; i < imagesSrc.length; i++) {
            document.getElementById("image" + ((i + 1).toString())).src = "images/" + imagesSrc[i] + ".jpg";
        }
    </script>

and you are ready to go. Enjoy Coding if it solve your problem do mark it as answer.

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

Comments

0

Try hardcoding the list string in JavaScript. If it still breaks, the problem isn't the array, but something else

For example, you may be doing getElementById before the element exists.. in which case this code would need to be wrapped in an "on page ready" event. Or, there may be a javascript error upwards on the page that is halting execution.

6 Comments

My script is the bottom of the page so img tag already created.
Hmm, that makes it more likely js is hitting an error before that point and halting. There are no js errors reported in the console?
I tried hard coding list and list works.I think var imagesSrc = '<%=images%>'; the problem and i check .cs file and images array works too
If it's an array in the form ['a','b'] you don't want the '' around it. you'd want imagesSrc = <%=images%>
In that form it gave me syntax error.
|
0

I don't think you can pass an array directly from your codebehind to valid javascript. How about assigning your array to a delimited string, then using split() to create an array in javascript.

Something like this:

Codebehind:

public partial class TestPage: System.Web.UI.Page
{
    public string MyCSV { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {    
         string csv = "";
         foreach (var itm in myarray) //array created elsewhere
         {
             csv = csv + loc + ",";
         }
         MyCSV = csv.Substring(0, csv.Length - 1); //trailing comma
    }
}

aspx:

<script type="text/javascript">
  var csvstring = '<%= MyCSV %>';
  var csvArray = csvstring.split(',');
  //do stuff with array
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.