最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Looping through values from c# array using javascript - Stack Overflow

programmeradmin2浏览0评论

Okay, I've created an array in c# and want to access the values in a javascript for loop. Here's my global c# variables:

protected int count;
protected string[] arr = new string[20];

From there I add string values to the array in, let's say, the Page_Load() event.

And here's my ideal javascript code:

var count = <%= count %>;
for (var i = 0; i < count; i++) 
{
document.write(<%= arr[i] %>);
}

Now if I were to just use arr[0] that would pop up with the correct information for arr[0], so I know I've got that part right, but is there a way to use that javascript variable "i" inside the <%= %> tag?

Okay, I've created an array in c# and want to access the values in a javascript for loop. Here's my global c# variables:

protected int count;
protected string[] arr = new string[20];

From there I add string values to the array in, let's say, the Page_Load() event.

And here's my ideal javascript code:

var count = <%= count %>;
for (var i = 0; i < count; i++) 
{
document.write(<%= arr[i] %>);
}

Now if I were to just use arr[0] that would pop up with the correct information for arr[0], so I know I've got that part right, but is there a way to use that javascript variable "i" inside the <%= %> tag?

Share Improve this question asked Aug 23, 2011 at 0:29 Aaron VanderwielenAaron Vanderwielen 2373 silver badges17 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

The ASP.NET parts of the code execute on the server. JavaScript is executed in the browser. The server just sees the JavaScript as text, it is meaningless and non-functional. Only when the browser receives the page and interprets the JS is it executed.

Remember, the server and the client PC are two different systems connected by a network. If you want to process data from system A on system B, you need to send the data to B.

If you want to send the data in the array to the browser so it can use it in some JavaScript, you need to serialize the array.

Something like this:

var myArray = <% = new JavaScriptSerializer().Serialize(serverSideArray) %>;
for(var i = 0; i < myArray.length; i++) {
    document.write(myArray[i]);
}

Hope it is useful to you... It was run fine in my test

    <script language ="javascript">
        var count = <%= count %>;
        alert(count);

        <%
           for(int i=0;i<count;i++){
         %>
            document.write(<%= arr[i] %>);
        <%}%>

    </script>

Sadly, I don't think so; because the Javascript is evaluated at run-time in the browser, but the server block is evaluated at pile time on the server.

You can probably just expand the scope of your server block and just loop through arr in C#

I am going to get flamed for this, but here goes:

Server-side:

protected void Page_Load(object sender, EventArgs e)
{
    string[] arr = new string[] { "1", "2", "3" };

    StringArr = string.Join(",", arr.Select(a => string.Format("\"{0}\"", a)).ToArray());
}

protected string StringArr;

Client-side:

<script language="javascript" type="text/javascript">
    var arr = [<%=StringArr %>];

    alert(arr.length);
</script>

Let the ridicule begin.

发布评论

评论列表(0)

  1. 暂无评论