簡體   English   中英

asp.net下拉列表緩存巨大的數據

[英]asp.net dropdownlist caching for Huge data

我必須將近50k記錄綁定到我的asp.net下拉列表中,並且必須是可搜索的。 實施它的最佳方法是什么? 是否有任何緩存技術,以便在滾動時加載列表?欣賞建議。

請指教。

我建議利用jQuery的自動完成插件:

https://jqueryui.com/autocomplete/

它是可配置的,並且具有開箱即用的自動完成搜索功能。 它也可以使用您的遠程數據源(盡管您可能會考慮分頁API響應):

http://api.jqueryui.com/autocomplete/#option-source

在后端,創建一個controller action (如果您使用的是ASP.NET MVC)或一個page method (如果您使用的是ASP.NET Web窗體),它接收一個searchTerm參數並返回一個頂部數組(例如,100 )結果。

在前端,使用一個預輸入 / 自動完成插件如這一個 當用戶設置搜索詞時,對后端執行Ajax請求並顯示結果。 執行Ajax請求時 ,您還可以啟用和配置緩存。 不再需要優化。

使用自動完成文本框並從遠程API設置數據源,尤其是在處理大型數據集時。 這樣可以避免應用程序UI在每次字符搜索時被掛起。

參考鏈接: https//github.com/ghiden/angucomplete-alt

您可以通過使用Web服務來實現此目的。

首先在aspx頁面中添加以下代碼。

<div>
    <input type="text" value="" id="tbCountries" />
</div>

現在,使用以下代碼創建Web服務。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.>WebService {
    [WebMethod]
    public List<string> ShowCountryList(string sLookUP)
    {
        List<string> lstCountries = new List<string>();

        string sConnString = "Data Source=DNA;Persist Security Info=False;" +
            "Initial Catalog=DNA_CLASSIFIED;User Id=sa;Password=;Connect Timeout=30;";

        SqlConnection myConn = new SqlConnection(sConnString);
        SqlCommand objComm = new SqlCommand("SELECT CountryName FROM Country " + 
            "WHERE CountryName LIKE '%'+@LookUP+'%' ORDER BY CountryName", myConn);
        myConn.Open();

        objComm.Parameters.AddWithValue("@LookUP", sLookUP);
        SqlDataReader reader = objComm.ExecuteReader();

        while (reader.Read()) {
            lstCountries.Add(reader["CountryName"].ToString());
        }
        myConn.Close();  return lstCountries;
    }
}

最后,使用webservice創建綁定Textbox的jquery方法,

<script>
    $(document).ready(function() {
        BindControls();
    });

    function BindControls() {
        $("#tbListOfCountries").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "WebService.asmx/ShowCountryList",
                    data: "{ 'sLookUP': '" + request.term + "' }",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function(data) { return data; },
                    success: function(data) {
                        response($.map(data.d, function(item) {
                            return { value: item }
                        }))
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },
            minLength: 1    // MINIMUM 1 CHARACTER TO START WITH.
        });
    }
</script>

取決於列表項的來源。 如果他們來自列表或數據庫只是附加他們然后使用JavaScript搜索列表。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>    

</head>
<body>
    <form id="form1" runat="server">
        <div class="ui-widget">
            <asp:TextBox ID="txtDepartment" runat="server"  ClientIDMode="Static" />            
        </div>
    </form>
    <script>       

        $(function () {

            $("[id$=txtDepartment]").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "FetchDropdownList.aspx/GetDepartment",
                        data: "{'departmentName':'" + document.getElementById('txtDepartment').value + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) { return data; },
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    value: item.Name
                                }
                            }))
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
                        }
                    });
                },
                minLength: 1
            });
        });        
    </script>
</body>
</html>

public class Department
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        private static List<Department> GetDepartment()
        {
            List<Department> departments = new List<Department>();
            for (int i = 0; i < 10000; i++)
            {
                Department department = new Department();
                department.Id = i;
                department.Name = "Department " + i.ToString();
                departments.Add(department);
            }
            return departments;
        }

        [WebMethod]
        public static List<Department> GetDepartment(string departmentName)
        {            
            int totalDepartments = GetDepartment().Count;
            List<Department> departments = GetDepartment().Where(d => d.Name.ToLower().StartsWith(departmentName.ToLower())).Take(20).ToList();

            return departments;
        }

我遇到了和你一樣的問題,我使用了RadAutoCompleteBox 它有許多客戶端和服務器端事件,可以幫助您處理各種情況。 它非常適合ASP.NET項目。

我看到兩個直接的解決方案。

  1. 正如其他人建議使用ajax搜索相關項目作為用戶類型並顯示它們。
  2. 在頁面加載時獲取所有數據並將它們存儲在javascript變量中,然后您可以對該變量執行搜索作為用戶類型並將搜索結果綁定到下拉列表。

相似:

絕對任何自動完成實現都適用於您的場景。

解決方案1:使用自動填充選擇框

  • 如果您不想浪費帶寬或想要支持低規格的設備,則應該使用服務器端數據獲取來進行自動完成。
  • 如果您想要高可用性而不關心帶寬,則可以將自動完成功能與本地數據一起使用(一次獲取50k記錄並綁定到自動完成)。 但要確保您不會一次將所有這些繪制到DOM中。 您需要限制在特定時間顯示的記錄。

解決方案2 :使用select with virtual

  • 但是如果你想為你的客戶提供最好的可用性 ,你應該選擇一個解決方案,你的選擇框被虛擬化 ,數據在滾動選擇框時被加載到DOM中。 通過虛擬化,您可以確保只有那些項目被推送到DOM ,這些項目在那個時間點顯示在屏幕上。

    你可以在這里找到一個基於jQuery的虛擬選擇

    在類似的實現作出反應在這里

    在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM