简体   繁体   English

从服务器端asp.net注册客户端json的有效方法?

[英]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.

相关问题 如何在ASP.NET中从客户端调用服务器端函数? - How to call server-side function from client-side in ASP.NET? 如何从客户端调用服务器端ASP.NET事件 - how to call a server-side ASP.NET event from the client-side 客户端(jquery)和服务器端(asp.net)应用程序都可以访问的服务 - Service that can be accessed by both client-side (jquery) and server-side (asp.net) applications Asp.Net:在服务器端还原DropDownList的客户端SelectedItem - Asp.Net: Restoring client-side SelectedItem of DropDownList on server-side 结合 Asp.Net Core 服务器端和 React 客户端 - Combining Asp.Net Core server-side and React client-side 在客户端加密ASP.NET文本字段,并在服务器端解密 - Encrypting ASP.NET text fields at client-side and decrypting them at server-side ASP.NET客户端与服务器端验证 - ASP.NET Client-side vs Server-side Validation 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? ASP.NET双列表框使用JQuery更新了客户端,以及如何在服务器端检索结果 - ASP.NET dual listboxes updated client-side with JQuery, and how to retrieve the results server-side 用于客户端和服务器端验证的 ASP.NET Core 重用代码 - ASP.NET Core Reuse Code for Client-Side and Server-Side Validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM