动态地创建与C#code JavaScript数组背后 [英] Dynamically create javascript array with c# code behind
问题描述
我更新的旧传统的ASP网站到一个新的.NET 3.5版本。该页面有该客户端(我的老板)要保持一个自定义的列表控件。该列表控制要求才能正常工作的几个阵列。该阵列是出版物的多维列表。这是什么样子:
I am updating an old classic ASP site to a new .net 3.5 version. The page has a custom list control which the client (my boss) wants to keep. This list control requires several arrays in order to work correctly. the array is a multi-dimensional list of publications. This is what it looks like:
var publicationTable = [
[31422,"Abilene Reporter News","Abilene","TX",false,"D",0],
[313844,"Acadiana Weekly","Opelousas","LA",false,"W",1],
[527825,"Action Advertiser","Fond du Lac","WI",false,"W",2]...n]
我要生成这个数组服务器端,并注册。我已经看了看<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerarraydeclaration.aspx\"相对=nofollow> MSDN 但这是有点微不足道。概念性问题是,数组和字符串整数的混合物,我不知道如何重现这一点,所以怎么样?
I want to generate this array server side and register it. I have looked at the msdn but this is a little trivial. The conceptual issue is that the array is a mixture of string and ints and I am not sure how to recreate this, so how?
推荐答案
您应该这样做:
背后code:
using System.Web.Script.Serialization;
...
public string getJson(){
var publicationTable = new List<object>{
new []{ 31422,"Abilene Reporter News","Abilene","TX",false,"D",0},
new []{ 313844,"Acadiana Weekly","Opelousas","LA",false,"W",1 },
new []{ 527825,"Action Advertiser","Fond du Lac","WI",false,"W",2}
};
return (new JavaScriptSerializer()).Serialize(publicationTable);
}
万一你看,创建混合类型的数组,我们创建匿名类型与数组新的[]
。你也可以用已经做到了新的对象[]
。
Ase you see, to create an array of mixed types, we create an array of anonymous type with new []
. You could also have done it with new object[]
.
aspx文件:
<script>
var publicationTable = <%= getJson() %>;
</script>
希望这有助于。干杯
Hope this helps. Cheers
这篇关于动态地创建与C#code JavaScript数组背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!