Forum Moderators: open

Message Too Old, No Replies

use Drop Down List to show panel

         

IntegrityWebDev

3:02 pm on Jan 27, 2010 (gmt 0)

10+ Year Member



I'm a relative ASP.NET (C#) newbie but I have a question I'm hoping someone can help with. I have a form that has a drop down list on it...if the end-user selects a certain answer from that form, I want to show a hidden field.

Maybe there's a better way but I assumed I would use a hidden panel that I would make visible if a certain field is selected on the drop down...but I'm not having any luck.

Here is the form snippet:


<asp:DropDownList ID="ddHeardAboutUs" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddHeardAboutUs_SelectedIndexChanged">
<asp:ListItem>Billboard</asp:ListItem>
<asp:ListItem>Search Engine</asp:ListItem>
</asp:DropDownList>

<asp:panel id="searchenginedetail" runat="server" Visible="false">
<label for="user">Which Search Engine:</label>
<asp:DropDownList ID="Craigslist" runat="server">
<asp:ListItem>Google</asp:ListItem>
<asp:ListItem>Yahoo</asp:ListItem>
</asp:DropDownList>
</asp:panel>

Here is the code:


protected void ddHeardAboutUs_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (ddHeardAboutUs.SelectedItem.Text == "Search Engine")
{
searchenginedetail.Visible = true;
}
}

When I change dropdown list the page does reload but the hidden pannel never shows. Any thoughts...or a better way to do this?

Thanks!

marcel

3:32 pm on Jan 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi IntegrityWebDev, I just tested your code and it works fine here, is there any other code setting the visible property of your Panel?

One thing that might make your life easiser is the following, instead of:

if (ddHeardAboutUs.SelectedItem.Text == "Search Engine")
{
searchenginedetail.Visible = true;
}

you can use
searchenginedetail.Visible = (ddHeardAboutUs.SelectedItem.Text == "Search Engine");

Which will switch the visibility, depending on the chosen list item, but I would recommend getting the selected value and not the Text here, could save you some headaches in the future.

IntegrityWebDev

5:54 pm on Jan 27, 2010 (gmt 0)

10+ Year Member



Thanks Marcel....I'm not sure what was going on in the code, but I changed it to yours, which *IS* more logical and compact, and now it works. There was other code i didn't list out that had been tweaked in the meantime...maybe that was it.

Either way, its working now...thanks!