Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 相同的自动完成在一个解决方案中有效,但在另一个解决方案中无效_C#_Jquery_Asp.net_Autocomplete - Fatal编程技术网

C# 相同的自动完成在一个解决方案中有效,但在另一个解决方案中无效

C# 相同的自动完成在一个解决方案中有效,但在另一个解决方案中无效,c#,jquery,asp.net,autocomplete,C#,Jquery,Asp.net,Autocomplete,我什么都试过了,这快把我逼疯了。 我做了一个在解决方案中工作的自动完成,但是当我导出它时,复制并粘贴到我的主解决方案中,它就不再工作了,我不断得到以下错误:无法获取未定义或空引用的属性“length”。 它不可能是jquery版本之类的,因为它可以在其他解决方案中工作 aspx: CodiceFiscale.aspx <link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/them/redmo

我什么都试过了,这快把我逼疯了。 我做了一个在解决方案中工作的自动完成,但是当我导出它时,复制并粘贴到我的主解决方案中,它就不再工作了,我不断得到以下错误:无法获取未定义或空引用的属性“length”。 它不可能是jquery版本之类的,因为它可以在其他解决方案中工作

aspx: CodiceFiscale.aspx

<link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/them/redmond/jquery-ui.css" />            
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>

<script type="text/javascript">
$(function () {
    $("#MainContent_provatxt").autocomplete({
        source: function (request, response) {
            var param = { cityName: $('#MainContent_provatxt').val() };
            $.ajax({
                url: "CodiceFiscale.aspx/GetCities",
                data: JSON.stringify(param),
                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
                        }
                    }))
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 3
    });
});
啊,顺便说一句,是的,我必须通过MainContent获取元素,因为浏览器中文本框的id不同于项目中的id,但在另一个解决方案中效果很好

代码隐藏:CodiceFiscale.aspx.cs

[WebMethod]
    public static List<string> GetCities(string cityName)
    {

        List<string> City = new List<string>();
        string query = string.Format("SELECT DISTINCT nome_comune FROM comuni WHERE nome_comune LIKE '%{0}%'", cityName);
        using (MySqlConnection conn = new MySqlConnection("server=localhost;Database=servizi; Uid=root; Pwd=root;"))
        {
            using (MySqlCommand cmd = new MySqlCommand(query, conn))
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    City.Add(reader.GetString(0));
                }
            }
        }
        return City;
    }

您可以使用ClientIDMode控制客户端id,以使id更可预测。
服务器在成功函数中返回什么?

我得到的参数未定义。数据中有一个异常:System.InvalidOperationExceptions所以在表达式$'MainContent\u provatxt'.val中似乎有一个错误的id。你能用浏览器开发工具验证这个id吗?我应该在哪里找到它?我通常用inspector单击文本框,但在这种情况下,如果自动完成初始化,则没有文本框,而id是正确的。在源函数中尝试执行数据:request.term。默认情况下,自动完成在用户键入3个字符后发送第一个请求。可以通过minLength属性对其进行控制。request.term必须包含相应请求中文本框中的所有文本。From:一个请求对象,具有一个术语属性,它引用文本输入中当前的值。例如,如果用户在城市字段中输入new yo,则自动完成项将等于new yo。
[WebMethod]
    public static List<string> GetCities(string cityName)
    {

        List<string> City = new List<string>();
        string query = string.Format("SELECT DISTINCT nome_comune FROM comuni WHERE nome_comune LIKE '%{0}%'", cityName);
        using (MySqlConnection conn = new MySqlConnection("server=localhost;Database=servizi; Uid=root; Pwd=root;"))
        {
            using (MySqlCommand cmd = new MySqlCommand(query, conn))
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    City.Add(reader.GetString(0));
                }
            }
        }
        return City;
    }