简体   繁体   English

ASP.NET到HTML5 localStorage

[英]ASP.NET to HTML5 localStorage

In ASP.NET (web forms), I am retrieving a set of key/value pairs from an SQL database to be used in a DropDownList. 在ASP.NET(Web表单)中,我从SQL数据库中检索一组键/值对,以用于DropDownList。 This list is quite large (can be over 2000 entries) and used multiple times over many of the pages on the website and I've been looking into ways to cache this set of data on the local client to reduce bandwidth. 该列表非常大(可以输入2000多个条目),并且在网站上的许多页面上使用了多次,并且我一直在研究在本地客户端上缓存这组数据以减少带宽的方法。 The list doesn't change often so having a cached copy a week or more should be fine. 该列表不会经常更改,因此拥有一周或更长时间的缓存副本应该没问题。

I wanted to know if it was at all possible and/or practical to have this list stored using HTML5 local storage and have a DropDownList fill from the client storage. 我想知道使用HTML5本地存储存储此列表并从客户端存储填充DropDownList是否完全可行和/或可行。 If no data if found locally, then going on to query the database for the list. 如果在本地找不到数据,则继续在数据库中查询列表。

If you have over 2000 entries in a select box it dosnt sound all that usable anyway. 如果在选择框中有2000多个条目,则无论如何都不会发出任何声音。

Have a look at something like Select2. 看一下类似Select2的东西。 Particularly the "Loading Remote Data" example. 特别是“加载远程数据”示例。

http://ivaynberg.github.io/select2/ http://ivaynberg.github.io/select2/

I have been involved in writing a couple of apps where data is pushed back and forth regularly and we built an API object in Javascript to handle data being pulled from the server on request. 我参与编写了一些应用程序,这些应用程序定期来回推送数据,我们在Javascript中构建了一个API对象,以处理应要求从服务器提取的数据。

The API module requested data and took an action depending on its success status. API模块请求数据并根据其成功状态采取措施。 You could build a simple API module to use if you feel that you may need to expand the type of data returning later, or just build a single AJAX call to pull the data for the drop down list. 如果您觉得可能需要扩展稍后返回的数据类型,则可以构建一个简单的API模块来使用,或者仅构建一个AJAX调用来提取下拉列表中的数据。

An example of the API interface would be as such: API接口的示例如下:

/** 
 * Parameter 1, string - Command Name for the server to interpret 
 * Parameter 2, object - Data that should be passed to the server (if necessary)
 * Parameter 3, string - the HTTP method to use: 'GET', 'POST', 'PUT' etc.
 * Parameter 4, function - the callback to fire once the response is received.
**/
api('CommandName', { key: 'value' }, 'METHOD', function(response) {
    if(response.success) {
         //Store data in localStorage here
    }
});

As you stated above, you are using the data multiple times throughout the pages on your website. 如上所述,您在网站的整个页面上多次使用数据。 Therefore in the JavaScript you would write a check on load which determines if the data has been stored within the localStorage, and if not makes a call to the API to populate it. 因此,在JavaScript中,您将编写负载检查,以确定数据是否已存储在localStorage中,如果没有,则调用API进行填充。 This would ensure that the client always has that data available. 这将确保客户端始终具有可用的数据。 This can be done like so: 可以这样完成:

//On Load
if(!localStorage.dropdown) {
    api('CommandName', { key: 'value' }, 'METHOD', function(response) {
        if(response.success) {
            localStorage.dropdown = response.data;
        }
    });
}

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

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