1

Good day!

I have a simple code here:

<asp:DropDownList ID="ddlNumber" runat="server">
   <asp:ListItem></asp:ListItem>
   <asp:ListItem>1</asp:ListItem>
   <asp:ListItem>2</asp:ListItem>
   <asp:ListItem>3</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="ddlText" runat="server">
   <asp:ListItem></asp:ListItem>
   <asp:ListItem>a</asp:ListItem>
   <asp:ListItem>b</asp:ListItem>
   <asp:ListItem>c</asp:ListItem>
</asp:DropDownList>

What I wanted to do is whenever a dropdown is not empty, the other dropdown is disabled.

The code I have tried so far isn't working:

        if (ddlNumber.SelectedValue == "" && ddlText.SelectedValue == "")
        {
            ddlNumber.Enabled = true;
            ddlText.Enabled = true;
        }
        else if (ddlNumber.SelectedValue != "")
        {
            ddlText.Enabled = false;
        }
        else if (ddlText.SelectedValue != "")
        {
            ddlNumber.Enabled = false;
        }

Any help would be much appreciated. Thank you.

2 Answers 2

3

You need to set Value property for the list items.

<asp:DropDownList ID="ddlNumber" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlNumber_SelectedIndexChanged">
   <asp:ListItem Value=""></asp:ListItem>
   <asp:ListItem Value="1">1</asp:ListItem>
   <asp:ListItem Value="2">2</asp:ListItem>
   <asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="ddlText" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlText_SelectedIndexChanged">
   <asp:ListItem Value=""></asp:ListItem>
   <asp:ListItem Value="a">a</asp:ListItem>
   <asp:ListItem Value="b">b</asp:ListItem>
   <asp:ListItem Value="c">c</asp:ListItem>
</asp:DropDownList>

Also handle SelectedIndexChanged event in .cs file

protected void Page_Load(object sender, EventArgs e)
{
    if (ddlNumber.SelectedValue == "" && ddlText.SelectedValue == "")
    {
        ddlNumber.Enabled = true;
        ddlText.Enabled = true;
    }
}

protected void ddlNumber_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlNumber.SelectedValue != "")
        ddlText.Enabled = false;
    else
        ddlText.Enabled = true;
}

protected void ddlText_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlText.SelectedValue != "")
        ddlNumber.Enabled = false;
    else
        ddlNumber.Enabled = true;
}
4
  • Tried and nothing happens
    – wobsoriano
    Commented Apr 24, 2016 at 1:55
  • @FewFlyBy updated with SelectedIndexChanged. Please check it now. Commented Apr 24, 2016 at 2:00
  • By the way, is there a code that can disable also the RequiredField for this dropdowns whenever it's disabled?
    – wobsoriano
    Commented Apr 24, 2016 at 2:10
  • Set ID to the RequiredFieldValidator and set Enabled property to "false" in .cs file <asp:RequiredFieldValidator runat="server" ID="rfvNumber" ControlToValidate="ddlNumber" ErrorMessage="*"> </asp:RequiredFieldValidator> In .cs file, rfvNumber.Enabled = false; Commented Apr 24, 2016 at 2:15
1

You should attach the event handlers and set AutoPostBack to true if you want the event to be triggered when a selection is made:

<asp:DropDownList ID="ddlNumber" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlNumber_SelectedIndexChanged" >
   <asp:ListItem></asp:ListItem>
   <asp:ListItem>1</asp:ListItem>
   <asp:ListItem>2</asp:ListItem>
   <asp:ListItem>3</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="ddlText" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlNumber_SelectedIndexChanged" >
   <asp:ListItem></asp:ListItem>
   <asp:ListItem>a</asp:ListItem>
   <asp:ListItem>b</asp:ListItem>
   <asp:ListItem>c</asp:ListItem>
</asp:DropDownList>

The event handler in code-behind:

protected void ddlNumber_SelectedIndexChanged(object sender, EventArgs e)
{
    ddlText.Enabled = ddlNumber.SelectedValue == "";
    ddlNumber.Enabled = ddlText.SelectedValue == "";
}
3
  • Thanks too! By the way, is there a code that can disable also the RequiredField for this dropdowns whenever it's disabled?
    – wobsoriano
    Commented Apr 24, 2016 at 2:10
  • 1
    Are you talking about a RequiredFieldValidator? If so, it also has an Enabled property that you can set to false. Commented Apr 24, 2016 at 2:13
  • Yes it is. Thank you sir!
    – wobsoriano
    Commented Apr 24, 2016 at 2:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.