我想在我的 c# asp.net 应用程序中实现 jquery 文本框自动建议以搜索员工。现在我在我的应用程序中使用 ajax 自动建议,但是当数据超过 5000 万时它似乎很慢。有人请帮助我。如果有任何关于在不使用索引的情况下使用大量数据更快地搜索的好主意,请与我分享。
user1485159
问问题
4762 次
3 回答
4
jQuery Auto 搜索详细信息如下:将此代码放入您的 .aspx 文件中。这里 txtSearchBox 是搜索框名称。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>AutoComplete Box with jQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
SearchText();
});
function SearchText() {
$("#txtSearch").autocomplete({
source: function(request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData",
data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
dataType: "json",
success: function(data) {
response(data.d);
},
error: function(result) {
alert("Error");
}
});
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel>
<ContentTemplate>
<div class="demo">
<div class="ui-widget">
<label for="tbAuto">Enter UserName: </label>
<asp:TextBox ID="txtSearch" runat="server" AutoCompleteType="Search"> </asp:TextBox>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
现在 .cs 文件详细信息:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List<string> GetAutoCompleteData(string username)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=devserver;Initial Catalog=Catalog;Persist Security Info=True;User ID=userName;Password=Password"))
{
using (SqlCommand cmd = new SqlCommand("select (strEmployeeName + ',' + strEmployeeCode) as username from tblEmployee where strEmployeeName LIKE '%'+@SearchText+'%' ", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["username"].ToString());
}
return result;
}
}
}
}
如果您希望可以使用对象数据源,例如:在这种情况下,您的对象方法应该返回 List 类型的数据。
[WebMethod]
public static List<string> GetAutoCompleteData(string strSearchKey)
{
AutoSearch_BLL objAutoSearch_BLL = new AutoSearch_BLL();
List<string> result = new List<string>();
result = objAutoSearch_BLL.AutoSearchEmployeesData(strSearchKey);
return result;
}
于 2012-07-29T03:09:09.190 回答
1
jQuery UI 提供了一个很好的自动完成实现,文档建议它可以用来从非常大的数据库中提取自动完成建议
您可以从本地和/或远程源提取数据:本地适用于小型数据集(例如具有 50 个条目的地址簿),远程对于大数据集是必要的,例如具有数亿或数百万条目的数据库从中选择。
http://jqueryui.com/demos/autocomplete/
请注意,如果您向浏览器返回50K 建议(而不是从 50K 可能性池中提取建议),那么您做错了(需要通过网络推送大量数据)。
于 2012-07-29T02:43:07.850 回答
1
实现jQuery UI 自动完成就这么简单
$(function(){
$( "#txtEmployee" ).autocomplete({
source: "employees.aspx",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
jQuery UI 自动完成支持数据的某些客户端caching
。这肯定会提高搜索速度。此外,您可以考虑在存储员工列表的应用程序中实现一个缓存层,以便自动完成不会每次都查询您的数据库。相反,它将从缓存层获取数据。
于 2012-07-29T02:43:40.867 回答