【问题标题】:Complete TextBox AutoComplete Example – Getting data from database完整的文本框自动完成示例 - 从数据库中获取数据
【发布时间】:2015-09-02 20:46:06
【问题描述】:

我正在尝试使用数据库中的名称自动完成文本框。当我在运行时输入文本框时,什么也没有发生。 我一直在关注本教程,但无法使其正常工作。 http://www.dotnetodyssey.com/2015/01/14/autocomplete-textbox-using-jquery-asp-net-querying-database-complete-example/

源代码如下。 Aspx 页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script type="text/javascript" src="jquery-1.11.2.min.js"></script>

<script type="text/javascript" src="jquery-ui.min.js"></script>

<link href="jquery-ui.css" rel="stylesheet" type="text/css"/>

<script type="text/javascript">

    $(document).ready(function () {

        $("#txtNames").autocomplete({

            source: function (request, response) {

                $.ajax({

                    type: "POST",

                    contentType: "application/json; charset=utf-8",

                    url: "WebForm1.aspx/GetNames",

                    data: "{'namePrefix':'" + $("#txtNames").val() + "'}",

                    dataType: "json",

                    minLength: 2,

                    success: function (data) {

                        response(data.d)

                    },

                    error: function (response) {

                        alert("Error" + res.responseText);

                    }

                });

            }

        });

    });

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <label for="txtNames">Names:</label>

<asp:TextBox ID="txtNames" runat="server"></asp:TextBox>
    </div>
    </form>
</body>


</html>

背后的代码:

 [WebMethod]

        public static string[] GetNames(string namePrefix)
        {

            List<string> Name = new List<string>();

            DataTable dtNames = new DataTable();

            string sqlQuery = "select distinct Name from Houses where Name like '" + namePrefix + "%'";

            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            try
            {

                SqlConnection conn = new SqlConnection(connectionString);

                SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);

                da.Fill(dtNames);

                foreach (DataRow row in dtNames.Rows)
                {

                    string name = Convert.ToString(row["Name"]);

                    Name.Add(name);

                }

            }

            catch (Exception ex)
            {

                throw ex;

            }

            return Name.ToArray<string>();

        } 

【问题讨论】:

  • 它是否击中了您的 WebMethod?此外,您告诉 ajax 您希望从该方法返回 json,但您没有返回 json。

标签: c# jquery sql asp.net json


【解决方案1】:

这是一个自动完成文本框的例子:

 private void LoadServices()
    {
        txtServiceName.AutoCompleteMode = AutoCompleteMode.Suggest;
        txtServiceName.AutoCompleteSource = AutoCompleteSource.CustomSource;
        txtServiceName.AutoCompleteCustomSource = colValues;
    }

还有:

AutoCompleteStringCollection colValues = new AutoCompleteStringCollection();
    private void GetAllServices()
    {
        // get list of Windows services
        ServiceController[] services = ServiceController.GetServices();
        List<string> ac = new List<string>();
        // try to find service name
        foreach (ServiceController service in services)
        {
            ac.Add(service.ServiceName.ToString());
        }
        colValues.AddRange(ac.ToArray());
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2019-05-19
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多