1

我有一个用于输入员工姓名的文本框。当用户输入员工姓名时,我想列出数据库中以输入的字母开头的所有员工,就像下拉列表一样

我不想要任何第三方控制。有没有更简单易懂的方法来做到这一点?

4

3 回答 3

1

你可以使用这个库,它很容易使用好的代码

http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx

于 2012-08-23T09:34:10.917 回答
0

首先,我们将使用将消耗的Jquery UI AuocompleteASP.NET Web Service

(比方说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 ServiceC#

[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

            }

注意:我没有对此进行测试,但我认为根据您的需要进行一些更改就可以正常工作!也尝试改进此代码

于 2012-08-23T09:57:15.850 回答
0

您可以使用 jquery 自动完成 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
   });
   });
于 2012-08-23T09:58:29.180 回答