在asp.net中自动完成,jquery与文本框无法正常工作

时间:2016-07-17 10:21:38

标签: c# jquery asp.net web-services autocomplete

Web FORM1代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ESEPAK.WebForm1" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">

        <title></title>
        <script src="jquery-1.11.2.js"></script>
         <script src="jquery-ui.js"></script>
         <script type="text/javascript">
         $(document).ready(function () {

                $('#txtName').autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            url: 'WebService1.asmx/GetDisplayData',
                            method: 'post',
                            contentType: 'application/json,charset=utf-8',
                            data: JSON.stringify({
                                term: request.term
                            }),
                            dataType: 'json',
                            success: function (data) {

                                response(data.d)
                            },
                            error: function (err) {

                                alert(err);
                            }


                        });
                    }
                });
            });
        </script>
        </head>
    <body>
        <form id="form1" runat="server">  Student Name : <asp:TextBox ID="txtName" runat="server"> </asp:TextBox> 
        </body>
    </html>

WEBSERVICE1.ASMX 代码

namespace ESEPAK
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public List<string> GetDisplayData( string term)
        {
            List<string> listStudentNames = new List<string>();
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; 
            using ( SqlConnection con = new SqlConnection(cs)) 
            {
                SqlCommand cmd = new SqlCommand("SpGetProductNames ", con); 
                cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add(new SqlParameter(){


                    ParameterName = "@term",
                    Value = term 
                });
                con.Open();



                SqlDataReader rdr = cmd.ExecuteReader(); 
                while (rdr.Read()) 
                { listStudentNames.Add(rdr["Name"].ToString());
                } 
            } return listStudentNames;



        }
    }
}t

我有一个参数化的存储过程名为&#39; term&#39;和 一切都很好项目正在运行但是自动完成文本框不起作用我有二十九千条记录名称这就是为什么j查询中的响应需要时间?我在WebForm1.aspx.cs页面和页面加载中没有添加任何内容。谁能告诉我它为什么不起作用?

0 个答案:

没有答案