initialize javascript array value from c#

1.3k Views Asked by At

I want to initialize JavaScript array value from c# in asp.net application. I have below code which i iterate through the loop,

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),  "assignGroupParty" + i, string.Format("javascript:SetProspectGroupPartyID({0},{1});"currentIndex, currentQueue.PartyID), true);

here

javascript:SetProspectGroupPartyID

is function which sets the array value at the currentIndex position.

 O/P:- [undefined, 37316]

it always assign at only one index and the other remains undefined. Can any one help me out on this. Thanks

1

There are 1 best solutions below

0
Oleks On BEST ANSWER

You could use JavaScriptSerializer class for this:

int[] numbers = new int[] { 1, 2, 3, 4, 5};
var serializer = new JavaScriptSerializer();
var jsArray = string.Format("var jsArray = {0}", serializer.Serialize(numbers));

and then register it by using e.g. ClientScriptManager.RegisterStartupScriptBlock method:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(
    this.GetType(), 
    "arrayDeclaration", 
    jsArray, 
    true
);