简体   繁体   English

如何创建文本框列表控件?

[英]how to create textbox list control?

I've a textbox for entering employeename.When an user types the employeename,i wanted to list all the employees from database starting with that typed letter,just like a dropdownlist 我有一个用于输入employeename的文本框。当用户键入employeename时,我想从数据库中以该键入的字母开头列出所有雇员,就像一个下拉列表一样

I dont want any third party control.Is there any easier and understandable way for doing this?? 我不希望任何第三方控制。是否有任何更容易理解的方法?

You can use this library it's very easy a with good code 您可以使用此库,只需编写好代码即可,非常简单

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

Firstly we will use Jquery UI Auocomplete that will consume ASP.NET Web Service 首先,我们将使用将消耗ASP.NET Web Service Jquery UI Auocomplete

(Let's say Name.asmx ). (假设Name.asmx )。

Add reference to Jquery UI CSS file 添加对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"/>

Now add reference to Jquery library 现在添加对Jquery library引用

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Add reference to the Jquery UI 添加对Jquery UI引用

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script> 

Let's say you have a textbox with class auto 假设您有一个带有auto类的文本框

<asp:TextBox ID="tbAuto" class="auto" runat="server">
</asp:TextBox>

Now we will write a script that will pass every single letter typed in the textbox to our Web Service (Name.asmx) . 现在,我们将编写一个脚本,该脚本会将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>

Finally here is the Web Service in C# 最后是C#Web Service

[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.
}

Hope it will help. 希望它会有所帮助。

Reply in case you have issues. 如果有问题,请回复。

EDITED: 编辑:

Here is the query. 这是查询。

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

            }

Note:I have not tested this , but I think by doing few changes as per your needs it will work fine! 注意:我尚未对此进行测试,但我认为,根据您的需要进行一些更改即可正常工作! Also try and improve this code 同时尝试改进此代码

you can use jquery autocomplete api : 您可以使用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
   });
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM