Olegs Jasjko
Olegs Jasjko

Reputation: 2088

Can't change selected option

I have a dropdownlist with data-bind;

<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 page is loaded, by default (as I understand, are selected first option). The issue is that I can't change this selection at all.

I tried second 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.

Actually, all variants seems to be working. They change visible value to chosen one (Car or Air) BUT when I'm trying to get selected value, I am getting value="" (-- Please Select--).

What can it be when dropdown show that one of options was selected (Car or Air) but the real selected option are still (-- Please Select --)?

Upvotes: 3

Views: 1030

Answers (1)

Dalorzo
Dalorzo

Reputation: 20014

Try this:

$('#<%=cmbType.ClientID %> option[value=' + d.Type + ']').attr("selected", "selected");

ClientID: Gets the control ID for HTML markup that is generated by ASP.NET.

You may also change the ClientIDMode of the control to Static which will preserve the value you set in your Id property.

Upvotes: 5

Related Questions