开发者

initialize javascript array value from c#

开发者 https://www.devze.com 2023-02-26 15:44 出处:网络
I want to initialize JavaScript array value from c# in asp.net application. I have below code which i iterate through the loop,

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
);
0

精彩评论

暂无评论...
验证码 换一张
取 消