简体   繁体   English

启用和禁用下拉列表

[英]Enabling & Disabling Drop Down Lists

I have a web form with two drop down lists. 我有一个带有两个下拉列表的Web表单。

  1. The 1st drop down list contains 2 values & 第一个下拉列表包含2个值和
  2. the 2nd drop down list contains 3 values. 第二个下拉列表包含3个值。

My objective is, when I select the 1st value of the 1st drop down list , the 2nd dropdownlist should not be visible , but when i select the 2nd value of the 1st dropdownlist , the 2nd dropdownlist should appear . 我的目标是,当我选择1st value 1st drop down list1st value时, 2nd dropdownlist应该不visible ,但是当我选择2nd value一个dropdownlist的第二2nd value时,应该appear第二个dropdownlist

You need to set the AutoPostBack-property of your first drop down list to true and add an OnSelectedIndexChanged-EventHandler 您需要将第一个下拉列表的AutoPostBack-property设置为true并添加OnSelectedIndexChanged-EventHandler

<asp:DropDown id="FirstList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="FirstList_Changed"></asp:DropDown>

In your EventHandler you can check the selected index and act accordingly. 在您的EventHandler中,您可以检查选定的索引并采取相应的措施。

protected void FirstList_Changed(object sender, EventArgs e) {
    if(FirstList.SelectedIndex == 0) {
        SecondList.Visible = false;
    } else {
        SecondList.Visible = true;
    }
}

However you can also do the same thing with JavaScript (see BPX's solution). 但是,您也可以使用JavaScript执行相同的操作(请参阅BPX的解决方案)。

You can do it by using JQuery. 您可以使用JQuery做到这一点。 A sample code is given below. 下面给出了示例代码。

$("#DropDownList1").change(function(){
    indx = $("#DropDownList1 option:selected").index();
    if(indx==1)
    {
      $("#DropDownList2").hide();
    }
    else
    {
      $("#DropDownList2").show();
    }
})

Don't forget to add jquery plugin. 不要忘记添加jquery插件。

If you want to enable and disable you can use this Code 如果要启用禁用 ,则可以使用此代码

 <asp:DropDownList ID="ddl1" runat="server" AppendDataBoundItems="true" AutoPostBack="true" Width="100%">
                    <asp:ListItem Text="" ></asp:ListItem>
                    <asp:ListItem Text="Value 1" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Value 2" Value="2"></asp:ListItem>
                </asp:DropDownList>

You need to set AutoPostBack="true" and in the SelectedIndexChanged write this code 您需要设置AutoPostBack =“ true”并在SelectedIndexChanged中编写此代码

 protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {

            try
            {
                if (ddl1.SelectedValue == "1")
            {
                ddl2.Enabled = false;
                //ddl2.Visible = false;
            }
            else
            {
                ddl2.Enabled = true;
                //ddl2.Visible = true;
            }
            }
            catch (Exception ex)
            {
               string b=  ex.Message;
            }

    }

For VISIBLE * enabling/disabling * USE the commented line and comment the other line 对于VISIBLE * 启用/禁用 *使用注释的行并注释另一行

Hope this helps 希望这可以帮助

Happy coding 快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM