Pass ASP Array variable to Javascript

Discussion in 'ASP .Net' started by =?Utf-8?B?QXVndXN0byBDZXNhcg==?=, Nov 6, 2003.

  1. Hello people.

    How can I Pass ASP Array variable to Javascript;

    I´m to trying this:

    <script language="JavaScript">

    var x = new Array(10);

    x = <%= myArrayAsp %>;

    </script>

    but return error to me...
     
    =?Utf-8?B?QXVndXN0byBDZXNhcg==?=, Nov 6, 2003
    #1
    1. Advertising

  2. What ASP.NET sends to the browser is plain text, not objects. You need to
    keep in mind that you have two different programming models - one on the
    server, and one on the client. If you aren't writing the text for the client
    side on the server-side code, then the client isn't getting it. The code you
    display here assumes that the client can, in some way, communicate with the
    server and get that variable, and that this variable will be type
    compatible. Neither assumption is true.

    Instead, you need your server side code to write out the code to initialize
    this array. Instead of just databinding to the array type, you'll have to
    iterate through the array and write it all out:

    (my vbscript skills are very rusty, so don't type this in directly, but you
    get the idea...)

    var x = new Array(10);
    <%
    dim i
    for i = 0 to Len(myArrayAsp) - 1
    ' Assumes 0-based on client and 1-based on server
    Response.Write "x[" & i & "] = " & myArrayAsp(i + 1) & ";"
    next i
    %>


    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows XP
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    "Augusto Cesar" <> wrote in message
    news:...
    > Hello people.
    >
    > How can I Pass ASP Array variable to Javascript;
    >
    > I´m to trying this:
    >
    > <script language="JavaScript">
    >
    > var x = new Array(10);
    >
    > x = <%= myArrayAsp %>;
    >
    > </script>
    >
    > but return error to me...
     
    Chris Jackson, Nov 6, 2003
    #2
    1. Advertising

  3. Hi,

    Chris is totally right. I started working on this before I saw Chris' post.
    He's got the solution for you. My solution is just a variation on his. All
    mine adds is how to run through the array after it's on the client. It also
    uses a couple different techniques.

    * place a text box on an ASPX page
    * Add the following to the code-behind
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim Fruit() As String = {"Apples", "Bananas", "Grapes", "Pears"}
    Dim s As New System.Text.StringBuilder
    s.Append("<script>var arr = new Array(")
    Dim i As Integer
    For i = 0 To Fruit.Length - 1
    s.Append("'" & Fruit(i) & "'")
    If i < (Fruit.Length - 1) Then
    s.Append(",")
    End If
    Next
    s.Append(");</script>")
    RegisterClientScriptBlock("Fruit", s.ToString)

    s = New System.Text.StringBuilder
    s.Append("<script>var i; var s = '';")
    s.Append("for (i = 0; i < arr.length; i++)")
    s.Append("{s = s + arr + ' '}")
    s.Append(" document.all('TextBox1').value = s;</script>")
    RegisterStartupScript("Show", s.ToString)
    End Sub


    Thank you, Mike
    Microsoft, ASP.NET Support Professional

    Microsoft highly recommends to all of our customers that they visit the
    http://www.microsoft.com/protect site and perform the three straightforward
    steps listed to improve your computer’s security.

    This posting is provided "AS IS", with no warranties, and confers no rights.


    --------------------
    > From: "Chris Jackson" <chrisjATmvpsDOTorgNOSPAM>
    > References: <>
    > Subject: Re: Pass ASP Array variable to Javascript
    > Date: Thu, 6 Nov 2003 15:58:41 -0500
    > Lines: 55
    > X-Priority: 3
    > X-MSMail-Priority: Normal
    > X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    > X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    > Message-ID: <>
    > Newsgroups: microsoft.public.dotnet.framework.aspnet
    > NNTP-Posting-Host: 208.252.52.2
    > Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    > Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:189110
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >
    > What ASP.NET sends to the browser is plain text, not objects. You need to
    > keep in mind that you have two different programming models - one on the
    > server, and one on the client. If you aren't writing the text for the

    client
    > side on the server-side code, then the client isn't getting it. The code

    you
    > display here assumes that the client can, in some way, communicate with

    the
    > server and get that variable, and that this variable will be type
    > compatible. Neither assumption is true.
    >
    > Instead, you need your server side code to write out the code to

    initialize
    > this array. Instead of just databinding to the array type, you'll have to
    > iterate through the array and write it all out:
    >
    > (my vbscript skills are very rusty, so don't type this in directly, but

    you
    > get the idea...)
    >
    > var x = new Array(10);
    > <%
    > dim i
    > for i = 0 to Len(myArrayAsp) - 1
    > ' Assumes 0-based on client and 1-based on server
    > Response.Write "x[" & i & "] = " & myArrayAsp(i + 1) & ";"
    > next i
    > %>
    >
    >
    > --
    > Chris Jackson
    > Software Engineer
    > Microsoft MVP - Windows XP
    > Windows XP Associate Expert
    > --
    > More people read the newsgroups than read my email.
    > Reply to the newsgroup for a faster response.
    > (Control-G using Outlook Express)
    > --
    >
    > "Augusto Cesar" <> wrote in message
    > news:...
    > > Hello people.
    > >
    > > How can I Pass ASP Array variable to Javascript;
    > >
    > > I´m to trying this:
    > >
    > > <script language="JavaScript">
    > >
    > > var x = new Array(10);
    > >
    > > x = <%= myArrayAsp %>;
    > >
    > > </script>
    > >
    > > but return error to me...

    >
    >
    >
     
    Mike Moore [MSFT], Nov 6, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    702
    Eric Enright
    Nov 15, 2004
  2. Mike
    Replies:
    2
    Views:
    7,456
    Eliyahu Goldin
    Jan 15, 2008
  3. Tricky
    Replies:
    0
    Views:
    599
    Tricky
    Mar 19, 2009
  4. Tricky
    Replies:
    0
    Views:
    1,044
    Tricky
    Mar 19, 2009
  5. Reuven
    Replies:
    0
    Views:
    606
    Reuven
    Mar 19, 2009
Loading...

Share This Page