initialize javascript array value from c#
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(), "assig开发者_高级运维nGroupParty" + 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
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
);
精彩评论