我有一个用于输入employeename的文本框。当用户键入employeename时,我想列出从该类型字母开始的数据库中的所有员工,就像下拉列表
我不想要任何第三方控制。有没有更容易和可理解的方法来做到这一点?
答案 0 :(得分:1)
您可以使用此库,使用优质代码非常容易
http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
答案 1 :(得分:0)
首先,我们将使用消耗ASP.NET Web Service
的 Jquery UI Auocomplete
(我们说Name.asmx
)。
添加对Jquery UI CSS
文件
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
现在添加对Jquery library
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
添加对Jquery UI
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
假设您有一个类auto
<asp:TextBox ID="tbAuto" class="auto" runat="server">
</asp:TextBox>
现在我们将编写一个脚本,将textbox
中输入的每个字母都传递给Web Service
(Name.asmx)
。
<script type="text/javascript">
$(function() {
$(".auto").autocomplete({
source: function(request, response) {
$.ajax({
url: "Name.asmx/FetchNames",
data: "{ 'name': '" + 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.Name
}
}))
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 2
});
});
</script>
最后,这里是Web Service
C#
[WebMethod]
public List<string> FetchNames(string name)
{
//Write your query to get names that begin with the letter passed as parameter and return results
//Remember return the results by converting it to List.
}
希望它会有所帮助。
如果您遇到问题,请回复。
<强>编辑:强>
这是查询。
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;
List<string> empName=new List<string>();
try
{
// Open connection to the database
string ConnectionString = "server=yourservername;uid=sa;"+
"pwd=yourpswd; database=yourdatabase";
con = new SqlConnection(ConnectionString);
con.Open();
// Set up a command with the given query and associate
// this with the current connection.
string CommandText = "SELECT FirstName" +
" FROM Employees" +
" WHERE (FirstName LIKE +name%)";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
// Execute the query
rdr = cmd.ExecuteReader();
while(rdr.Read())
{
empName.Add(rdr["FirstName"].ToString());
}
return empName;
}
catch(Exception ex)
{
// Print error message
}
注意:我没有对此进行过测试,但我认为根据您的需要进行一些更改可以正常工作!同时尝试改进此代码
答案 2 :(得分:0)
你可以使用jquery autocomplete api:
$(document).ready(function () {
$("#<%=ApplicationSearchResult.ClientID %>").autocomplete({ //applicationSearchResult is your textbox
source: function (request, response) {
$.ajax({
type: "POST",
url: "Dashboard.aspx/GetTreeNodesByText", // the function you have to call to bring you the data
data: "{'text': '" + request.term + "'}", // the letter the user entered in the textbox
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
response(data.d);
}
});
},
minLength: 1
});
});