0

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?

3
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by coder91
0

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.

0

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.

0

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;
            }
0

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
0

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.

0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.