c# - Adding default value to dropdown in jquery ajax call -
i have cascading dropdowns. on selection of 1 dropdown, dropdown binds using ajax. works want second dropdown should contain default text , value ('select me'). how can add default value. code.
function bindcities(drpstate) { var stateid = $(drpstate).val(); $.ajax({ url: '/register.aspx/bindcities', type: "post", async: false, contenttype: "application/json; charset=utf-8", datatype: "json", data: '{"id":"' + stateid + '"}', success: function (result) { var drpcity = $('.drpcity'); drpcity.empty(); $.each(result.d, function () { drpcity.append( $('<option/>', { value: this.id, text: this.name }) ); }); }, error: function (xhr, status) { alert(status + " - " + xhr.responsetext); } }); }
html:
<asp:dropdownlist runat="server" cssclass="drpstate" onchange="bindcities(this)" id="drpdwnstate"> <asp:listitem text="select state" value="0"></asp:listitem> </asp:dropdownlist> <asp:dropdownlist runat="server" cssclass="drpcity" id="drpdwncity"> <asp:listitem text="select city" value="0"></asp:listitem> </asp:dropdownlist>
server side code:
[webmethod] public static list<countrystate> bindcities(int id) { ekbnmanager em = new ekbnmanager(); list<city> cities = em.getcitybystateid(id); list<countrystate> countrystate = new list<countrystate>(); foreach (var item in cities) { countrystate cnt = new countrystate(); cnt.name = item.name; cnt.id = item.id; countrystate.add(cnt); } return countrystate; }
in success function this:
success: function (result) { var drpcity = $('.drpcity'); drpcity.empty(); drpcity.append( $('<option/>', { value: -1, text: please select 1 }) $.each(result.d, function () { drpcity.append( $('<option/>', { value: this.id, text: this.name }) ); });
Comments
Post a Comment