Solved

Hide/Show Panel Control based on DropDownList value

Posted on 2013-01-18
5
2,419 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now