Passing an array from code behind to Javascript

A

Ant

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
 
N

Nathan Sokalski

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.
 
A

Ant

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 said:
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
(e-mail address removed)
http://www.nathansokalski.com/

Ant said:
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
 
G

George Ter-Saakov

Check out ClientScriptManager.RegisterArrayDeclaration method

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

George.
 
N

Nathan Sokalski

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
(e-mail address removed)
http://www.nathansokalski.com/

Ant said:
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 said:
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
(e-mail address removed)
http://www.nathansokalski.com/

Ant said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top