在ASP.Net C#中使用JQuery - 自动完成文本框

时间:2018-04-23 20:52:34

标签: c#

我正在尝试使用数据库表中的项自动填充文本框;但它不是自动完成的。即使有数据,也没有信息没有从数据库中提取。我不同的做法是自动完成功能不起作用。

<head runat="server">

 <link href="jquery-ui.css" rel="stylesheet" type="text/css" />  
<script src="jquery.min.js" type="text/javascript"></script>  
<script src="jquery-ui.min.js" type="text/javascript"></script>  

<script type="text/javascript">  
    $(document).ready(function() {  
        SearchText();  
    });  
    function SearchText() {  
        $("#txtInvoiceNum").autocomplete({  
            source: function(request, response) {  
                $.ajax({  
                    type: "POST",  
                    contentType: "application/json; charset=utf-8",  
                    url: "LoadSheet.aspx/GetInvoice",  
                    data: "{'invoiceNum':'" + document.getElementById('txtInvoiceNum').value + "'}",  
                    dataType: "json",  
                    success: function(data) {  
                        response(data.d);  
                    },  
                    error: function(result) {  
                        alert("No Match");  
                    }  
                });  
            }  
        });  
    }  
</script>   

<asp:TextBox ID="txtInvoiceNum" runat="server"></asp:TextBox>

  [WebMethod]
public static List<string> GetInvoice(string invoiceNum)
{
    List<string> empResult = new List<string>();
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TWCL_OPERATIONSConnectionString"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select Top 10 SOPNUMBE from [TWCL].[dbo].[SOP10100] where  SOPNUMBE LIKE ''+@SearchInvoice+'%'";
            cmd.Connection = con;
            con.Open();
            cmd.Parameters.AddWithValue("@SearchInvoice", invoiceNum);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                empResult.Add(dr["SOPNUMBE"].ToString());
            }
            con.Close();
            return empResult;
        }
    }
}

0 个答案:

没有答案