[英]Access server-side list to javascript
I have datarow written from the database.how can I access the list in javascript? 我有从数据库写的数据行。如何使用javascript访问列表? Please help.
请帮忙。
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("var testArray = new Array;");
foreach (DataRow str in result)
{
sb.Append("testArray.push('" + str.ItemArray[1].ToString() + "');");
}
sb.Append("</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "test('"PassTestArrayHere"');", true);
Your script will not work, instead of using sb.Append("testArray.push('" + str.ItemArray[1].ToString() + "');");
您的脚本无法正常运行,而不是使用
sb.Append("testArray.push('" + str.ItemArray[1].ToString() + "');");
you can convert you array to JSON and return to client. 您可以将数组转换为JSON并返回到客户端。 In case you still want to follow you approach, you should convert your array to a string with the format like:
String a = "['item0', 'item1', 'item2']";
如果仍要遵循该方法,则应将数组转换为具有以下格式的字符串:
String a = "['item0', 'item1', 'item2']";
and then: 接着:
Page.ClientScript.RegisterStartupScript(String.GetType(), "test", "test(" + a + ");", true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.