Jimushi
Jimushi

Reputation: 121

When a certain item on dropdownlist1 is selected, select automatically a certain item on dropdownlist2

I want to when the item x5 is selected on dropdownlist1 the item y0 is automatically selected on dropdownlist2

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_Itemchanged">
                        <asp:ListItem Value='5'>x5</asp:ListItem>
                        <asp:ListItem Value="4">x4</asp:ListItem>
                        <asp:ListItem Value="3">x3</asp:ListItem>
                        <asp:ListItem Value="2">x2</asp:ListItem>
                        <asp:ListItem Value="1">x1</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="DropDownList2" runat="server">
                        <asp:ListItem Value="0.75">y0.75</asp:ListItem>
                        <asp:ListItem Value="0.50">y0.50</asp:ListItem>
                        <asp:ListItem Value="0.25">y0.25</asp:ListItem>
                        <asp:ListItem Value="0">y0</asp:ListItem>
</asp:DropDownList>

protected void DropDownList1_Itemchanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem.Value == "5")
        {
            DropDownList2.Items.FindByValue("0").Selected = true;
            DropDownList2.Items.FindByValue("0.75").Attributes.Add("Disabled", "Disabled");
            DropDownList2.Items.FindByValue("0.50").Attributes.Add("Disabled", "Disabled");
            DropDownList2.Items.FindByValue("0.25").Attributes.Add("Disabled", "Disabled");
        }
    }

When I select the item x4 on dropdownlist1 and select the item y0.25 on dropdownlist2, and after that when I select x5 on the dropdownlist1 it gives me "Cannot have multiple items selected in a DropDownList"

Upvotes: 2

Views: 47

Answers (1)

Bart van der Drift
Bart van der Drift

Reputation: 1336

Use the SelectedValue property on the list:

protected void DropDownList1_Itemchanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.Value == "5")
    {
        DropDownList2.SelectedValue = "0";
    }
}

Upvotes: 1

Related Questions