[英]Fill javascript array with c#
How Can i fill an array that defined in javascript with c# in behind code? 如何在后面的代码中用c#填充javascript中定义的数组?
EDIT: 编辑:
here is my code 这是我的代码
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 "";
}
}
Javsacript: Javsacript:
<script type="text/javascript">
$(function () {
var usernames = new Array();
$("#tags").autocomplete({
source: usernames
});
});
</script>
use the JavaScriptSerializer
class. 使用JavaScriptSerializer
类。 Something like the following should do it 像下面这样的东西应该这样做
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.