ASP.NET-後端值傳給前端javascript(ASP.NET code-behind value pass to javascript )
會遇到這個需求是需要後端撈DB資料傳給前端javascript畫圖使用,提供一個方法可達到此功能。
後端
using System.Web.Script.Serialization;
protected string value = "test";
public static class JavaScript
{
public static string Serialize(object o)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(o);
}
}
前端
<script type="text/javascript">
var value = <%=JavaScript.Serialize(this.value) %>;
</script>
後端
using System.Web.Script.Serialization;
protected string value = "test";
public static class JavaScript
{
public static string Serialize(object o)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(o);
}
}
前端
<script type="text/javascript">
var value = <%=JavaScript.Serialize(this.value) %>;
</script>
留言
張貼留言