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