Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hide/Show Panel Control based on DropDownList value

Posted on 2013-01-18
5
Medium Priority
?
3,008 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

876 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