Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Populate dropdown using jQuery and KnockOut

0.00/5 (No votes)
5 Sep 2012 1  
This WIKI displays how to populate a dropdown using KNockOutJS with jQuery.KNockOutJs is a JavaScript Library which used MVVM pattern and provides

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

This WIKI displays how to populate a dropdown using KNockOutJS with jQuery.

KNockOutJs is a JavaScript Library which used MVVM pattern and provides multiple functionality like Client Side and Server side code integration, Dynamic Data Binding, Dynamic UI etc. You can find more information about KnockOut from http://knockoutjs.com/ .

So, At First add reference of jQuery and KnockOut Library to page

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="knockout.js"></script>

Put following  markup

<p>
        Your country:
	<asp:DropDownList ID="ddlCountries" runat="server" data-bind="options: countryModel.countries, optionsValue: 'CountryID', optionsText: 'CountryName', optionsCaption: 'Choose...'">
        </asp:DropDownList>
 </p>
 <input type="button" value="Add" data-bind="click: addCountry" />

Add script tag to page as follows:

<script type="text/javascript">
        function DropDownModel() {
            var self = this;
            self.countries = ko.observableArray();
            self.addCountry = function () {
                $.ajax({
                    type: "POST",
                    url: "DropDownSolution.aspx/GetCountries",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        self.countries(data.d);
                    }
                });
            };
        }
        var countryModel = new DropDownModel()
        ko.applyBindings(countryModel);
    </script>

In this script DropDownSolution.aspx is aspx page which contains a page method with the name GetCountries.

Add following code as your page method:

        [WebMethod]
        public static List<Country> GetCountries()
        {
            List<Country> countries = new List<Country>();
            countries.Add(new Country { CountryID = "1", CountryName = "India" });
            countries.Add(new Country { CountryID = "2", CountryName = "Singapore" });
            countries.Add(new Country { CountryID = "3", CountryName = "Malaysia" });
            return countries;
        }

you can implement any logic in GetCountries() Method and return all records in a List.

REF:

 


 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here