Passing an array from code behind to Javascript

Passing an array from code behind to Javascript

am 04.04.2008 03:12:00 von 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

Re: Passing an array from code behind to Javascript

am 04.04.2008 06:53:41 von 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.
--
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

"Ant" wrote in message
news:040C0A7D-EC7D-4F80-B3A4-E3CCE960A01F@microsoft.com...
> 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
>
>

Re: Passing an array from code behind to Javascript

am 04.04.2008 07:06:00 von 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" 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
> njsokalski@hotmail.com
> http://www.nathansokalski.com/
>
> "Ant" wrote in message
> news:040C0A7D-EC7D-4F80-B3A4-E3CCE960A01F@microsoft.com...
> > 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
> >
> >
>
>
>

Re: Passing an array from code behind to Javascript

am 04.04.2008 15:03:39 von George Ter-Saakov

Check out ClientScriptManager.RegisterArrayDeclaration method

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

George.


"Ant" wrote in message
news:040C0A7D-EC7D-4F80-B3A4-E3CCE960A01F@microsoft.com...
> 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
>
>

Re: Passing an array from code behind to Javascript

am 04.04.2008 20:53:47 von 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
njsokalski@hotmail.com
http://www.nathansokalski.com/

"Ant" wrote in message
news:3B2A16A9-2C06-4BEC-8B29-FD6FCA8D61B3@microsoft.com...
> 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
>> njsokalski@hotmail.com
>> http://www.nathansokalski.com/
>>
>> "Ant" wrote in message
>> news:040C0A7D-EC7D-4F80-B3A4-E3CCE960A01F@microsoft.com...
>> > 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
>> >
>> >
>>
>>
>>