gpt4 book ai didi

c# - 用c#填充javascript数组

转载 作者:数据小太阳 更新时间:2023-10-29 05:59:27 24 4
gpt4 key购买 nike

如何在后面的代码中用 C# 填充在 javascript 中定义的数组?

编辑:

这是我的代码

protected void Page_Load(object sender, System.EventArgs e)
{
string[] locations = new string[] {
"Las Vegas",
"Los Angeles",
"Tampa",
"New York",
"s",
"sss"
};
string jsArray = GetJSArrayForVBArray(locations);
this.ClientScript.RegisterArrayDeclaration("usernames", jsArray);
}

private string GetJSArrayForVBArray(string[] vbArray)
{
StringBuilder myResult = new StringBuilder();
foreach (string item in Constants.vbArray) {
{
myResult.Append(",'" + item + "'");
}
}
if ((myResult.Length > 0)) {
return myResult.ToString().Substring(1);
} else {
return "";
}
}

Javascript:

<script type="text/javascript">
$(function () {
var usernames = new Array();
$("#tags").autocomplete({
source: usernames
});
});
</script>

最佳答案

使用JavaScriptSerializer类(class)。像下面这样的事情应该做

protected void Page_Load(object sender, System.EventArgs e)
{
string[] locations = new string[] {
"Las Vegas",
"Los Angeles",
"Tampa",
"New York",
"s",
"sss"
};

JavaScriptSerializer serializer = new JavaScriptSerializer();

string jsArray = serializer.Serialize(locations);
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "locations", jsArray, true);
}

关于c# - 用c#填充javascript数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3429869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com