简体   繁体   中英

Efficient way to register client-side json from server-side asp.net?

I wrote an aspx page. I want to register a client var from the code behind (c#) directly to the markup . 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. So I prefer as little conversions as possible (not converting to string and than parsing to json on the client side).

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; 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>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM