[英]Efficient way to register client-side json from server-side asp.net?
I wrote an aspx page. 我写了一个aspx页面。 I want to register a client var from the code behind (c#) directly to the markup .
我想从(c#)后面的代码直接将客户var注册到标记 。 Meaning no communication is needed.
意味着不需要通信。
I have converted some user-defined object to json object on the server side, and the client (jquery) expects to use json obejct var. 我已经在服务器端将一些用户定义的对象转换为json对象,并且客户端(jquery)期望使用json obejct var。 So I prefer as little conversions as possible (not converting to string and than parsing to json on the client side).
因此,我希望转换尽可能少(在客户端不转换为字符串,而不是解析为json)。
What is the most efficient and elegant way to do so ? 这样做最有效,最优雅的方法是什么?
I am not 100% sure I follow your question, but apparently, you are looking to write a javascript variable from server side; 我不是100%肯定会遵循您的问题,但是显然,您正在寻找从服务器端编写javascript变量的方法; something like:
就像是:
var json = @"var myobj={""Name"":""John Smith"",""Age"",34};";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myvar", json,true);
This will render this on client-side: 这将在客户端呈现:
<script type="text/javascript">
//<![CDATA[
var myobj={"Name":"John Smith","Age",34};//]]>
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.