我编写了这段代码,用于将数组从asp.net代码传递给javascript:
aspx文件:
<script type="text/javascript">
var loc_array;
var loc_array_two;
function AssignValues(loc, loc_two){
loc_arary= loc;
loc_array_two = loc_two;
alert(loc_array[0]);
}
</script>
aspx.cs文件:
StringBuilder sb = new StringBuilder();
sb.Append("var loc = [");
foreach (var str in lb1) //lb1 is list
{
sb.Append("['" + str.Value1 + "','" + str.Value2 + "','" + str.Value3 + "'],");
}
sb.Append("];");
StringBuilder sb2 = new StringBuilder();
sb2.Append("var own = [");
foreach (var str2 in lb2) //lb2 is list
{
sb2.Append("['" + str2.Value1 + "','" + str2.Value2 + "','" + str2.Value3 + "'],");
}
sb2.Append("];");
if (lb1.Count > 0 && lb2.Count > 0)
{
string arrayStr1 = string.Format("[{0}]", sb.ToString().TrimEnd('{','}'));
string arrayStr2 = string.Format("[{0}]", sb.ToString().TrimEnd('{', '}'));
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "AssignValues(" + arrayStr1 + "," + arrayStr2 + ")", true);
}
创建的数组是[var loc = [['New York','33.6306686','73.1175653'],];]
- 现在这个数组传递给了javascript函数。但没有打印。任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
为什么不使用此表单?
var yourjavascriptArray = <%=new JavaScriptSerializer().Serialize(cSharpArrayName);%>;