自动完成javascript无法使用asp控件在母版页中工作

时间:2015-05-13 11:02:26

标签: javascript jquery asp.net

此脚本在单独的Web表单中工作。但是相同的脚本在使用母版页的页面中不起作用。

我在单独的文件中尝试了这个....如果我调用相同的脚本,则无法正常工作。

添加了webmethod

<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#txtAutoComplete").autocomplete({
        source: function(request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Haha.aspx/GetCategory",
                data: "{'term':'" + $("#txtAutoComplete").val() + "','term1':'" + $("#txtAutoComplete1").val() + "'}",
                dataType: "json",
                success: function(data) {
                    response(data.d);
                },
                error: function(result) {
                    alert("Error");
                }
            });
        }
    });
    $("#txtAutoComplete1").autocomplete({
        source: function(request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Haha.aspx/GetCategory1",
                data: "{'term':'" + $("#txtAutoComplete").val() + "','term1':'" + $("#txtAutoComplete1").val() + "'}",
                dataType: "json",
                success: function(data) {
                    response(data.d);
                },
                error: function(result) {
                    alert("Error");
                }
            });
        }
    });
});    </script>

[的WebMethod]

    public static List<string> GetCategory(string term, string term1)
    {
        if (string.IsNullOrEmpty(term1))
            term1 = " ";
        List<string> result = new List<string>();
        Business.ClientBusinessLayerSQL ObjMgnr = new Business.ClientBusinessLayerSQL();
        DataSet ds = new DataSet();
        ds = ObjMgnr.fnLocationSearch(term, term1, "GET-SEARCH-1");
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            //result.Add(ds.Tables[0].Rows[0]["LocName"].ToString());
            result.Add(row["LocName"].ToString());
        }
        return result;
    }
    [WebMethod]
    public static List<string> GetCategory1(string term, string term1)
    {
        List<string> result = new List<string>();
        Business.ClientBusinessLayerSQL ObjMgnr = new Business.ClientBusinessLayerSQL();
        DataSet ds = new DataSet();
        ds = ObjMgnr.fnLocationSearch(term, term1, "GET-SEARCH-2");
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            result.Add(row["LocName"].ToString());
        }
        return result;
    }

1 个答案:

答案 0 :(得分:0)

确保您具有相同的文本框ID,因为放置在母版页内的控件会继承contentplaceholder id.So以供此用ClientIDMode="Static"。此外,当您在ajax调用中传递数据时,参数名称及其值应为in quotes,或者您应在ajax调用和WebMethod JSON.stringify中使用name of parametersshould be same。您的WebMethods应为static

您的自动填充js未加载。你是直接从互联网上提到的。下载文件并放入项目中并从那里引用。