Solved

Hide/Show Panel Control based on DropDownList value

Posted on 2013-01-18
5
2,641 Views
Last Modified: 2013-01-18
Hello Experts,

I need to show/hide a Panel Control on my web form based on the selected value selected from the DropDownList control. So for example if the user selects the value "Switch" from my DropDownList Control then I need to show the Panel Control called "pnlSwitch" and hide the rest of the Panel Controls. I remember doing this before but cannot find my documentation. Please help?? I know I need to add "SelectedIndexChanged" to the DropDownList control but not sure what to do after that :(

DropDownList Values:
70   Server
71   Switch
72   Modem

Panel Controls:
pnlServer
pnlSwitch
pnlModem
0
Comment
Question by:asp_net2
  • 3
  • 2
5 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38793753
pnlServer.Visible = false;
pnlSwitch.Visible = false;
pnlModem.Visible = false;
switch(ddl.SelectedText)
{
        case "Server":

                pnlServer.Visible = true;
                break;
                
}

Open in new window

0
 
LVL 4

Author Comment

by:asp_net2
ID: 38793890
Hi informaniac,

That worked out great. I had to modify it a little, see below but thank you. There is just one more question to ask. Could this have also been achieved using an "if" statement? If so, how? and why did you not use it here? Only asking so I understand.

Code:
    protected void ddlChooseClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlChooseClass.SelectedItem.Text)
        {
            case "Server":

                pnlServer.Visible = true;
                break;
        }
    }

Markup:
<asp:DropDownList ID="ddlChooseClass" CssClass="ddl" OnSelectedIndexChanged="ddlChooseClass_SelectedIndexChanged" AutoPostBack="true" runat="server"></asp:DropDownList>
0
 
LVL 4

Author Comment

by:asp_net2
ID: 38793895
Also, how do I add multiple values such as Switches, Routers, Printers, etc.. to the switch statement?
0
 
LVL 20

Accepted Solution

by:
informaniac earned 500 total points
ID: 38794093
You can use an If statement also. I used a switch only cause of code clarity. It's upto the developer how he prefers...

protected void ddlChooseClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        pnlServer.Visible = false;
        pnlPrinter.Visible = false;
        switch (ddlChooseClass.SelectedItem.Text)
        {
            case "Server":

                pnlServer.Visible = true;
                break;
            case "Printers":
                pnlPrinter.Visible = true;
                break;
        }
    }

Open in new window


protected void ddlChooseClass_SelectedIndexChanged(object sender, EventArgs e)
{
        pnlServer.Visible = false;
        pnlPrinter.Visible = false;
        string strSelectedText = ddlChooseClass.SelectedItem.Text;
        if(strSelectedText == "Printers")
        {
               pnlPrinter.Visible = true;
        }
        else if(strSelectedText == "Servers")
        {
                pnlServer.Visible = true;
        }
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 38794163
Thank you very much for your help and explanations.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question