预测性搜索不起作用? [英] Predictive search isn't working?
问题描述
这是aspx文件代码.
This is aspx file code.
<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() {
$(".autosuggest").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">
<div class="demo"></div>
<div class="ui-widget">
<label for="tbAuto">Enter UserName: </label>
<input type="text" id="txtSearch" class="autosuggest" />
</div>
</form>
</body>
</html>
This is .cs file code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;
public partial class demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static string getconnectionstring()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["crudconnection"].ConnectionString;
}
public static List<string> GetAutoCompleteData(string username)
{
List<string> result = new List<string>();
SqlConnection con = new SqlConnection(getconnectionstring();
using (SqlCommand cmd = new SqlCommand("select DISTINCT username from crudtable where username LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["username"].ToString());
}
return result;
}
}
}
数据库"crud"的"crudtable"列(用户名,名字,姓氏,地址,id)具有列.当我在文本框中键入任何内容并按pres按钮时,搜索不起作用.
一切似乎都是正确的,但不知道错误在哪里.
帮助朋友.谢谢
The database "crud" has "crudtable" with columns (username,firstname,lastname,address,id).When i type anything in the textbox and pres button the ,Search isn''t working.
Everything seems to be right but don''t know where is the error.
Help friends.Thanks
推荐答案
(document).ready(function(){ SearchText(); }); 函数SearchText(){
(document).ready(function () { SearchText(); }); function SearchText() {
(.autosuggest").autocomplete({ 来源:函数(请求,响应){
(".autosuggest").autocomplete({ source: function (request, response) {
.ajax({ 输入:"POST", contentType:"application/json; charset = utf-8", 网址:"Default.aspx/GetAutoCompleteData", 数据:"{'username':'" + document.getElementById('txtSearch').value +'}", dataType:"json", 成功:功能(数据){ 响应(data.d); }, 错误:函数(结果){ alert("Error"); } }); } }); } < /script > < /head > < 正文 > < 表单 =" form1" runat >服务器" > < div =" 演示" < > < div =" ui-widget" < 标签 =" > 输入用户名:< /label < 输入 =" 文本" id txtSearch" 类 =" 自动建议" / < /div > < /form > < /body > < /html > 这是.cs文件代码. 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControls; 使用System.Data.SqlClient; 使用System.Data; 使用System.Web.Services; 公共部分类演示:System.Web.UI.Page { 受保护的void Page_Load(对象发送者,EventArgs e) { } 公共静态字符串getconnectionstring() { 返回System.Configuration.ConfigurationManager.ConnectionStrings [&"crudconnection&"].ConnectionString; } 公共静态List& lt; string& gt; GetAutoCompleteData(字符串用户名) { 清单& lt;字符串& gt;结果=新的List& lt; string& gt;(); SqlConnection con =新的SqlConnection(getconnectionstring(); 使用(SqlCommand cmd = new SqlCommand(& quot;从可碎对象中选择DISTINCT用户名,其中用户名LIKE'%' + @ SearchText +'%'& quot; ,con)) { con.Open(); cmd.Parameters.AddWithValue(&"@ SearchText&",用户名); SqlDataReader dr = cmd.ExecuteReader(); 而(dr.Read()) { result.Add(dr [&用户名&"].ToString()); } 返回结果; } } }
.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"> <div class="demo"></div> <div class="ui-widget"> <label for="tbAuto">Enter UserName: </label> <input type="text" id="txtSearch" class="autosuggest" /> </div> </form> </body> </html> This is .cs file code. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.Web.Services; public partial class demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public static string getconnectionstring() { return System.Configuration.ConfigurationManager.ConnectionStrings["crudconnection"].ConnectionString; } public static List<string> GetAutoCompleteData(string username) { List<string> result = new List<string>(); SqlConnection con = new SqlConnection(getconnectionstring(); using (SqlCommand cmd = new SqlCommand("select DISTINCT username from crudtable where username LIKE '%'+@SearchText+'%'", con)) { con.Open(); cmd.Parameters.AddWithValue("@SearchText", username); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { result.Add(dr["username"].ToString()); } return result; } } }
数据库"crud"的"crudtable"列为(用户名,名字,姓氏,地址,id).当我在文本框中键入任何内容并按pres按钮时,搜索不起作用.
一切似乎都是正确的,但不知道错误在哪里.
帮助朋友.谢谢
The database "crud" has "crudtable" with columns (username,firstname,lastname,address,id).When i type anything in the textbox and pres button the ,Search isn''t working.
Everything seems to be right but don''t know where is the error.
Help friends.Thanks
这篇关于预测性搜索不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!