简体   繁体   English

在ASP.NET WebForms中将服务器变量传递给客户端的正确方法

[英]Proper way to pass server variables to client in ASP.NET WebForms

In the past, I usually have just embedded the values I need passed into html data elements and then read them from there via JavaScript. 过去,我通常只是将需要传递的值嵌入到html数据元素中,然后通过JavaScript从中读取它们。 However, I know there's a more proper way to do so. 但是,我知道有一种更合适的方法。 In particular, I'd like to learn how implement the first method mentioned in this blog post . 特别是,我想学习如何实现本博文中提到的第一种方法。 However, I'd also like to know what the best way to pass data from server-side to client side is when you're not using ajax. 但是,我也想知道从服务器端向客户端传递数据的最佳方法是不使用Ajax时。

EDIT: Since I'm still pretty unsure, let me explain what I'm trying do to. 编辑:由于我仍然不确定,所以让我解释一下我正在尝试做什么。 I'm creating a page with a lot of dynamic content on it that is dependent on reading from a database. 我正在创建一个页面,上面有很多动态内容,这取决于从数据库中读取内容。 I want to be able to load the page's static content first. 我希望能够首先加载页面的静态内容。 Then, using jQuery, I want to make an ayschronous HTTP get request to the server to read from the database and retrieve the information I need. 然后,我想使用jQuery向服务器发出一个异步HTTP get请求,以从数据库中读取并检索所需的信息。 Finally, if I need more information from the database, I want to be able to request the server for more information without having to reload the page. 最后,如果我需要数据库中的更多信息,我希望能够请求服务器获取更多信息而不必重新加载页面。

"When you're not using ajax"!?!? “当您不使用ajax时!!!!? - Start! -开始!

If you're dead set on not making a service request to the server, there are the following ways: 如果您不愿意向服务器发出服务请求,则可以采用以下几种方法:

Hidden form fields - so <input type=hidden value=xyz > 隐藏的表单字段-因此<input type = hidden value = xyz>

PageMethods - ok this is ajax, but it's simple, see here http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2> http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2 PageMethods-好的,这是ajax,但是很简单,请参见此处http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2> http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2

Best way.... is to do it properly, implement webapi and send down JSON then on the client end interpret that with Javascript. 最好的方法是...正确执行此操作,实现webapi并发送JSON,然后在客户端使用Javascript进行解释。

Edit: your edit paints a different picture. 编辑:您的编辑会绘制不同的图片。 I thought you just wanted a single piece of data, but it sounds like you need a proper api, so go with webapi, and use jQuery to make the calls, then populate through code, or jQuery templates(the best option). 我以为您只需要一个数据,但这听起来像您需要一个适当的api,因此请使用webapi,并使用jQuery进行调用,然后填充代码或jQuery模板(最佳选择)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串从javascript传递到服务器? 然后从asp.net webforms中的服务器获取返回值 - How to pass a string from javascript to server? and then get a return value from server in asp.net webforms 如何填补ASP.NET webforms中服务器端和客户端之间的空白? - How to fill the gap between server-side and client-side in ASP.NET webforms? 将Typescript添加到现有asp.net 4 Webforms项目的推荐方法 - Recommended way to add Typescript to an existing asp.net 4 Webforms project 在ASP.NET WebForms中在服务器端初始化bootsrap datatimepicker - Initializing bootsrap datatimepicker on server side in asp.net webforms 如何使用 fetch 将正确的 url 传递给 asp.net 控制器? - How to pass proper url using fetch to asp.net controller? 如何在Asp.Net MVC上实现客户端Ajax登录(Asp.Net Webforms解决方案的链接在这里) - How to implement a Client-side Ajax Login on Asp.Net MVC (A link to the solution for Asp.Net Webforms is in here) 在asp.net webforms中引用javascript文件 - Referencing javascript files in asp.net webforms 在ASP.NET Web表单中使用Bootbox提示符 - Using Bootbox prompt in ASP.NET webforms 浏览器错误(Firefox)Asp.net网络表单 - browser errors (firefox) Asp.net webforms AngularJS将数据发布到ASP.NET WebForms - AngularJS Post data to ASP.NET WebForms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM