Solved

Hide/Show Panel Control based on DropDownList value

Posted on 2013-01-18
5
2,781 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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