So have a drop down list with various values, one of them being "other". I have written code so that when "other" is selected a panel which has text boxes in it is made visible.

      if (ddlOption.SelectedItem.Text == "Other")
            {
                panelNext.Visible = true;
            }
            else
            {
                panelNext.Visible = false;
            }

This is the back end code I have.

Any suggestions?

Recommended Answers

All 9 Replies

So is it not working? What is the problem you are having?

Yea its not working, when I select other from the drop down list its just reloads the page and makes this drop down blank again. Whereas it's supposed to display a text box.

If the dropdown is set to autopost back, then you would have to include this code in the page load so that you detect the selection of "other".

Otherwise you can remove the autopost back and check for this selection when you hit the submit button for your form.

There are other events that are triggered that you can take advantage of to check to see if "other was selected" such as selected index change.

I have selected index changed in my code as a method that is what holds

 if (ddlOption.SelectedItem .Text == "Other")
            {
                panelNext.Visible = true;
            }
            else
            {
                panelNext.Visible = false;
            }

So, I was on the right track with the issue here but was confused on the specific way to resolve this in my previous post. The selectedIndexChanged event is correct to use, but you also have to set the dropdown list control for autopostback to True. Here is a working example. Its in VB but the code your C# code applies the same.

ASPX
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList runat="server" ID="ddlOption" OnSelectedIndexChanged="ddlOption_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem Text="Option 1" Value="Option 1" />
        <asp:ListItem Text="Option 2" Value="Option 2" />
        <asp:ListItem Text="Other" Value="Other" />
        </asp:DropDownList>
    </div>
        <asp:Panel runat="server" ID="panelNext" Visible="False">
            <p>Hello!</p>
        </asp:Panel>
    </form>
</body>
Code behind
Protected Sub ddlOption_SelectedIndexChanged(sender As Object, e As EventArgs)
    If ddlOption.SelectedValue = "Other" Then
        panelNext.Visible = True
    End If
End Sub

Yea I already have all off this in, only difference between my code and your code is I have an ObjectDataSource which is filling the drop down list. Im stumped as to why this isn't working. Only thing I can think off is that it's something to do with the autopostback possibly.

Start with a new page with this code, and then add in the different components until it breaks.

JorgeM please help me too ,I have also posted a post with a name

datalist checkbox selecting it

Turned out another member of my team who i was unable to talk to had used 0 in the drop down list to display Please select. I had set 0 in the database as the value for other so when i found this out was able to change the value to other to -1 and now the text boxes appear. Thanks for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.