Solved

Hide/Show Panel Control based on DropDownList value

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 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

9 Experts available now in Live!

Get 1:1 Help Now