![](/img/trans.png)
[英]How can I implement jQuery DataTables plugin using C#, ASP.NET, SQL Server side processing?
[英]How can i implement textbox auto suggest using jquery in my c# asp.net application?
我想在我的C#asp.net应用程序中实现jquery文本框自动建议以搜索员工。现在我在我的应用程序中使用ajax自动建议,但是当数据超过50000时似乎很慢,请有人帮帮我。如果有关于在不使用索引的情况下更快地处理海量数据的好主意,请与我分享。
jQuery自动搜索的详细信息如下:将此代码放在.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;
}
jQuery UI提供了很好的自动完成建议,文档建议它可以用于从大型数据库中提取自动完成建议
您可以从本地和/或远程源中提取数据:本地适用于小型数据集(例如具有50个条目的地址簿),远程适用于大型数据集,例如具有数百或数百万个条目的数据库从中选择。
http://jqueryui.com/demos/autocomplete/
请注意,如果您向浏览器返回 5万条建议(而不是从5万条可能性中提取建议),那么您做错了事(大量数据需要在线发送)。
如此实现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
。 那肯定会提高搜索速度。 另外,您可以考虑在存储“员工列表”的应用程序中实现缓存层 ,以便自动完成功能不会每次都查询数据库。 相反,它将从缓存层获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.