我曾尝试使用 jquery 和下面提到的代码来做自动完成文本框,但我无法实现它,我在 jquery 中遇到错误。提前致谢。
自动完成文本框.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoCompleteTextBox.aspx.cs" Inherits="Ajax_Using_Jquery.AutoCompleteTextBox" %>
<!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 runat="server">
<title>Ajax Auto Complete Using 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">
var UserNameInput;
$(document).ready(function () {
$("#<%=txtName.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
//contentType: "application/json; charset=utf-8",
url: "AutoCompleteTextBox.aspx/getUserNames",
data: "{'TextBoxVal':'" + document.getElementById('<%=txtName.ClientID%>').value + "'}",
dataType: "json",
success: function (data) {
alert("success");
},
error: function (result) {
alert("error ");
}
});
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<div>
User Name :
<asp:TextBox ID="txtName" runat="server" ></asp:TextBox>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
自动完成文本框.aspx.cs:
public List<string> getUserNames(string TextBoxVal)
{
string strCon;
List<string> objList = new List<string>();
strCon = ConfigurationManager.ConnectionStrings["EmpNameFetch"].ConnectionString;
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand(@"select EmpName from newTb2 where EmpName like '%"+TextBoxVal+"%'", con);
con.Open();
SqlDataReader objReader = cmd.ExecuteReader();
while (objReader.Read())
{
objList.Add(objReader["EmpName"].ToString());
}
Response.Write(objList.ToString());
con.Close();
return objList;
}