从所选值设置下拉列表值

时间:2020-08-10 13:42:40

标签: c# asp.net dropdown

我有一个下拉列表,其中包含一些值,例如C#ASP.Net中的“热水”和“电”。我想做的是,如果用户选择这些值之一并单击一个按钮,那么它将把第二个下拉列表的值设置为某些值。例如如果选择了“热水”并单击了“下一步”,则dropdownlist2中的值将为“散热器”和“加热控件”。但是,如果选择“ Electricity”,则dropdownlist2中的值为“ No Power”和“ Sockets”。到目前为止,我的代码:

protected void NextButton1_Click(object sender, EventArgs e)
{
    option1 = DropDownList1.SelectedValue.ToString();
    TextBox1.Text = option1;
    DropDownList2.Visible = true;
    if (DropDownList1.SelectedValue == "Hot Water")
    {
        DropDownList2.SelectedValue = "Heating controls";
        DropDownList2.SelectedValue = "Underfloor Heating";
    }
}

3 个答案:

答案 0 :(得分:1)

我希望我对以下内容有所了解:

if(DropDownList1.SelectedValue == "Hot Water"){
    DropDownList2.Items.Clear();
    DropDownList2.Items.Add("Radiator");
    DropDownList2.Items.Add("Heating Controls");
}
if(DropDownList1.SelectedValue == "Electricity"){
    // Do the same like above but for the values in Electricity
}

答案 1 :(得分:0)

首先检查像这样的DefaultAzureCredential按钮代码的dropdownlist1中是否选择了Hot WaterElectricity

Nextclick

答案 2 :(得分:0)

不要认为您可以在下拉列表中进行多选,因此请使用asp:ListControlasp:CheckBoxList

如果您想使用下拉式多选功能,可以使用许多JS控件来完成。 例如:https://harvesthq.github.io/chosen/

一旦您更新了列表代码,一切都将正常工作。

相关问题