I want to pass an object from my c# code behind to my javascript. I know that I can use
var myVar = '<%# myVar %>'
to pass variables. However, that method seems to pass everything as a string. I want an object.
Is there any way to accomplish that?
You can serialize it to JSON using the JavaScriptSerializer
.
Something like:
System.Web.Script.Serialization.JavaScriptSerializer oSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(myVar);
Then you in your aspx code you can use:
var myVar = <%# sJSON %>;
Which will output something like:
var myVar = {"Name":"John","Age":"30","ID":"111"};
使用JSON序列化将.NET对象转换为JS,可以将其反序列化为对象(或者,执行到对象中)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.