开发者

can i pass values to this Jquery var for autocomplete() from DB.?

开发者 https://www.devze.com 2023-03-30 15:42 出处:网络
$(function () { var availableTags = [ \"ActionScript\", \"AppleScript\",\"Asp\",\"BASIC\", \"C\",\"C++\",\"Clojure\",\"COBOL\",\"ColdFusion\",\"Erlang\",
$(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
    });
});

the snippet given above is from jquery.orq - can i make one a开发者_如何学运维rray (of available products that should be retrieved from DB - MS SQL Server in ASP.NET) .. ?


You have to write a method that return JSON and then call this method on your jquery script and build the js array with the result.


Ther's a lot of ways to accomplish this , the easiest way i think is to add a hiddenfield that u create server side that will contain the values retreived from your database.

//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.

and the jquery will be

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

Hope this helps.


 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);

And in Javascript need to the same:

    $(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
        });
    });


The best way is to use AJAX call for that.

In you ASP.NET application you should have .ASMX of WCF web service, that could return array of entities for autocomplete.

Just an example code, that might guide you.

Server-side (WCF):

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

On client side, you have to query your web service to have data,

var startWith = $('#input').val();
$.getJson('/WebService/GetProductsStartWith', startWith, function(response) {

  $("#mdatepicker").autocomplete({
        source: response.d
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消