Can't change the selected option
I have a data-bound dropdown,
<asp:DropDownList ID="cmbType" Runat="server" AutoPostBack="False" data-bind="value: moveType">
<asp:ListItem Value="">-- Please Select --</asp:ListItem>
<asp:ListItem Value="0">Car</asp:ListItem>
<asp:ListItem Value="1">Air</asp:ListItem>
</asp:DropDownList>
When the page is loaded, by default (as I understand the first option is selected). The problem is that I cannot change this choice at all.
I've tried doing other things:
$('#cmbType option[value=' + d.Type + ']').attr("selected", "selected");
or
$("#cmbType").val(d.Type);
or
var viewModel = {
this.moveType = ko.observable(d.Type);
};
ko.applyBindings(new ViewModel());
d.Type = 0 or 1.
In fact, all options work. They change the visible value to the selected one (Car or Air), but when I try to get the selected value, I get value = "" (- Select -).
What happens when the dropdown shows that one of the options is selected (Car or Air), but the actual option selected is still (- Please Select -)?
source to share
Try the following:
$('#<%=cmbType.ClientID %> option[value=' + d.Type + ']').attr("selected", "selected");
ClientID : Retrieves the ID of the control for HTML markup that is generated by ASP.NET.
You can also change the ClientIDMode
control to Static
, which will retain the value specified in the property Id
.
source to share