【问题标题】:can i pass values to this Jquery var for autocomplete() from DB.?我可以从 DB 将值传递给这个 Jquery var 以进行 autocomplete() 吗?
【发布时间】:2011-11-02 18:46:41
【问题描述】:
$(function () {
    var availableTags = [
            "ActionScript", "AppleScript","Asp","BASIC",
                     "C","C++","Clojure","COBOL","ColdFusion","Erlang",
                     "Fortran","Groovy","Haskell","Java","JavaScript","Lisp",
                     "Perl","PHP","Python","Ruby","Scala","Scheme"];
    $("#mdatepicker").autocomplete({
        source: availableTags
    });
});

上面给出的 sn-p 来自jquery.orq - 我可以制作一个数组(应该从数据库中检索的可用产品 - ASP.NET 中的 MS SQL Server).. ?

【问题讨论】:

标签: jquery asp.net sql-server-2005 autocomplete jquery-autocomplete


【解决方案1】:

您必须编写一个返回 JSON 的方法,然后在您的 jquery 脚本上调用此方法并使用结果构建 js 数组。

【讨论】:

  • 是否有任何函数从 List 返回为 JSON 字符串?
【解决方案2】:

有很多方法可以做到这一点,我认为最简单的方法是添加一个隐藏字段,您可以在服务器端创建一个隐藏字段,该字段将包含从您的数据库中检索到的值。

//connect to your database and retreive your data and insert it into hf.value.
hF.Value = "the values retreived must be separated by a ',' "
hF.ID = "hF";
// add your control to the webpage.

jquery 将是

$(function () {
    var availableTags = $("#hF").val().split(',');
    $("#mdatepicker").autocomplete({
        source: availableTags
    });
});

希望这会有所帮助。

【讨论】:

    【解决方案3】:
     List<string> myAutoList = new List<string>() ;
    
                myAutoList.Add("Grand Trust");
                myAutoList.Add("iSmart");
                myAutoList.Add("F5 Tech");
                StringBuilder  script = new StringBuilder();
                script.Append("var availableTags = [ ");
                foreach (string str in myAutoList )
                {
                    script.Append("'");
                    script.Append(str.ToString());
                    script.Append("', ");
                }
                script.Remove(script.Length - 2, 2);
                script.Append(" ];");
    
                ClientScript.RegisterClientScriptBlock(GetType(), "MyScript", script.ToString(), true);
    

    和在Javascript中需要一样:

        $(function () {
    //            if (availableTags == null)
    //            {
    //                var availableTags = [
    //                'ActionScript',   "AppleScript",  "Asp",  "BASIC","C","C++","Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell","Java",
    //                "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"];
    //            }
    
            $("#mdatepicker").autocomplete({
                source: availableTags
            });
        });
    

    【讨论】:

    • 喜欢这个查询,从数据库中获取字符串列表,并制作这个脚本字符串并使用 clientscript 注册。
    【解决方案4】:

    最好的方法是为此使用 AJAX 调用。

    在您的 ASP.NET 应用程序中,您应该拥有 WCF Web 服务的 .ASMX,它可以返回用于自动完成的实体数组。

    只是一个示例代码,可能会指导您。

    服务器端 (WCF):

    public IList<Product> GetProductsStartWith(string productName) {
       // ask db here and return results
       return productList;
    }
    

    在客户端,您必须查询您的网络服务以获取数据,

    var startWith = $('#input').val();
    $.getJson('/WebService/GetProductsStartWith', startWith, function(response) {
    
      $("#mdatepicker").autocomplete({
            source: response.d
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      相关资源
      最近更新 更多