Passing an array from code behind to Javascript

Discussion in 'ASP .Net' started by Ant, Apr 4, 2008.

  1. Ant

    Ant Guest

    Hi,
    I'd like to pass an array that is set set in the page load in code behind
    into client side javascript so that I cache the array on the client. Can this
    be done?

    Basically this is what I want to do (This doesn't work but for purposes of
    explanation):

    Code behind:
    protected void Page_Load(object sender, EventArgs e)
    {
    string[] myOP = {"test1", "test2"};
    }

    client javascript
    function buttonClientSide_onclick() {

    Array myOP = '<% = myOP %>';

    alert(myOP[1]);
    }

    How can this be done?
    Many thanks for any ideas
    Ant
     
    Ant, Apr 4, 2008
    #1
    1. Advertising

  2. In the PreRender event, generate the JavaScript code as a String, and then
    use the Page.ClientScript.RegisterClientScriptBlock() method (for the
    details on the parameters for this method, see the documentation). This will
    add the script to your generated document. When generating the String, you
    should be sure to be careful that you escape any control characters, it is
    usually a good idea to do a view source to make sure the desired JavaScript
    was generated, it is easy to make little mistakes when generating
    JavaScript. When generating the String, it is a good idea to try to make
    good use of the System.Text.StringBuilder and any appropriate String methods
    (in your case, it sounds like String.Join() may come in handy). Hopefully
    this information will help get you started.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Ant" <> wrote in message
    news:...
    > Hi,
    > I'd like to pass an array that is set set in the page load in code behind
    > into client side javascript so that I cache the array on the client. Can
    > this
    > be done?
    >
    > Basically this is what I want to do (This doesn't work but for purposes of
    > explanation):
    >
    > Code behind:
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > string[] myOP = {"test1", "test2"};
    > }
    >
    > client javascript
    > function buttonClientSide_onclick() {
    >
    > Array myOP = '<% = myOP %>';
    >
    > alert(myOP[1]);
    > }
    >
    > How can this be done?
    > Many thanks for any ideas
    > Ant
    >
    >
     
    Nathan Sokalski, Apr 4, 2008
    #2
    1. Advertising

  3. Ant

    Ant Guest

    Hi Nathan,

    Many thanks for that. A question, will this actually create an array cached
    on the client so that I won't need to postback to iterate through the array?

    Many thanks again

    Ant

    "Nathan Sokalski" wrote:

    > In the PreRender event, generate the JavaScript code as a String, and then
    > use the Page.ClientScript.RegisterClientScriptBlock() method (for the
    > details on the parameters for this method, see the documentation). This will
    > add the script to your generated document. When generating the String, you
    > should be sure to be careful that you escape any control characters, it is
    > usually a good idea to do a view source to make sure the desired JavaScript
    > was generated, it is easy to make little mistakes when generating
    > JavaScript. When generating the String, it is a good idea to try to make
    > good use of the System.Text.StringBuilder and any appropriate String methods
    > (in your case, it sounds like String.Join() may come in handy). Hopefully
    > this information will help get you started.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Ant" <> wrote in message
    > news:...
    > > Hi,
    > > I'd like to pass an array that is set set in the page load in code behind
    > > into client side javascript so that I cache the array on the client. Can
    > > this
    > > be done?
    > >
    > > Basically this is what I want to do (This doesn't work but for purposes of
    > > explanation):
    > >
    > > Code behind:
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > string[] myOP = {"test1", "test2"};
    > > }
    > >
    > > client javascript
    > > function buttonClientSide_onclick() {
    > >
    > > Array myOP = '<% = myOP %>';
    > >
    > > alert(myOP[1]);
    > > }
    > >
    > > How can this be done?
    > > Many thanks for any ideas
    > > Ant
    > >
    > >

    >
    >
    >
     
    Ant, Apr 4, 2008
    #3
  4. Check out ClientScriptManager.RegisterArrayDeclaration method

    So you can do
    string[] myOP = {"test1", "test2"};
    Page.ClientScript.RegisterArrayDeclaration("myOP", myOP);

    George.


    "Ant" <> wrote in message
    news:...
    > Hi,
    > I'd like to pass an array that is set set in the page load in code behind
    > into client side javascript so that I cache the array on the client. Can
    > this
    > be done?
    >
    > Basically this is what I want to do (This doesn't work but for purposes of
    > explanation):
    >
    > Code behind:
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > string[] myOP = {"test1", "test2"};
    > }
    >
    > client javascript
    > function buttonClientSide_onclick() {
    >
    > Array myOP = '<% = myOP %>';
    >
    > alert(myOP[1]);
    > }
    >
    > How can this be done?
    > Many thanks for any ideas
    > Ant
    >
    >
     
    George Ter-Saakov, Apr 4, 2008
    #4
  5. When the page is generated, this will be the same as if you had hardcoded
    the JavaScript in the *.aspx file. It may become clearer what it does if you
    do a view source in your browser (sometimes it takes a little searching to
    find the right place in the view source where the script was placed, but it
    will be there).
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Ant" <> wrote in message
    news:...
    > Hi Nathan,
    >
    > Many thanks for that. A question, will this actually create an array
    > cached
    > on the client so that I won't need to postback to iterate through the
    > array?
    >
    > Many thanks again
    >
    > Ant
    >
    > "Nathan Sokalski" wrote:
    >
    >> In the PreRender event, generate the JavaScript code as a String, and
    >> then
    >> use the Page.ClientScript.RegisterClientScriptBlock() method (for the
    >> details on the parameters for this method, see the documentation). This
    >> will
    >> add the script to your generated document. When generating the String,
    >> you
    >> should be sure to be careful that you escape any control characters, it
    >> is
    >> usually a good idea to do a view source to make sure the desired
    >> JavaScript
    >> was generated, it is easy to make little mistakes when generating
    >> JavaScript. When generating the String, it is a good idea to try to make
    >> good use of the System.Text.StringBuilder and any appropriate String
    >> methods
    >> (in your case, it sounds like String.Join() may come in handy). Hopefully
    >> this information will help get you started.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>
    >> "Ant" <> wrote in message
    >> news:...
    >> > Hi,
    >> > I'd like to pass an array that is set set in the page load in code
    >> > behind
    >> > into client side javascript so that I cache the array on the client.
    >> > Can
    >> > this
    >> > be done?
    >> >
    >> > Basically this is what I want to do (This doesn't work but for purposes
    >> > of
    >> > explanation):
    >> >
    >> > Code behind:
    >> > protected void Page_Load(object sender, EventArgs e)
    >> > {
    >> > string[] myOP = {"test1", "test2"};
    >> > }
    >> >
    >> > client javascript
    >> > function buttonClientSide_onclick() {
    >> >
    >> > Array myOP = '<% = myOP %>';
    >> >
    >> > alert(myOP[1]);
    >> > }
    >> >
    >> > How can this be done?
    >> > Many thanks for any ideas
    >> > Ant
    >> >
    >> >

    >>
    >>
    >>
     
    Nathan Sokalski, Apr 4, 2008
    #5
    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 Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    590
    Alphonse Giambrone
    Jun 28, 2003
  2. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,243
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  3. =?Utf-8?B?UGF1bA==?=
    Replies:
    5
    Views:
    20,598
    =?Utf-8?B?UGF1bA==?=
    Jul 5, 2005
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    489
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. A. Atienza

    Passing code/data from code-behind to ASPX page

    A. Atienza, Dec 8, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    318
    Eliyahu Goldin
    Dec 8, 2004
Loading...

Share This Page