Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

asp.net c# dropdownlist onselectedindexchanged displays specific panels

Posted on 2011-09-02
2
Medium Priority
?
428 Views
Last Modified: 2012-05-12
Hello,
I am attempting to program a dropdownlist linked to a databse table such that on postback, the user's selection displays a specific panel.

My aspx code follows:
-----------------
<asp:DropDownList ID="drpemptype" runat="server" CssClass="txtbox" Width="140px"
                                            AutoPostBack="True" DataTextField="TypeofPerson" DataValueField="TypeofPerson"
                                            OnSelectedIndexChanged="drpemptype_SelectedIndexChanged">
                                            <asp:ListItem Text="Select Type"></asp:ListItem>
                                        </asp:DropDownList>


<!--below this line is table with panels -->
                                <tr>
                                    <td colspan="9" class="centerpanel" style="border-top-style: solid; border-left-style: solid;
                                        border-top-width: 1px; border-left-width: 1px; border-top-color: #E4CB7B; border-left-color: #E4CB7B;">
                                        <br />
                                        <br />
                                        <asp:Panel ID="pnl_employee" runat="server" BackColor="#ededed" Height="200px" Width="100%"
                                            Visible="false">
                                            <asp:Label ID="lbl_pnlEmployee" runat="server" CssClass="successmsg">Panel: Employee</asp:Label>
                                        </asp:Panel>
                                        <br />
                                        <br />
                                        <asp:Panel ID="pnl_contractor" runat="server" BackColor="#cccccc" Height="200px"
                                            Width="100%" Visible="false">
                                            <asp:Label ID="lbl_pnlContractor" runat="server" CssClass="successmsg">Panel: Contractor</asp:Label>
                                        </asp:Panel>
                                        <br />
                                        <br />
                                        <asp:Panel ID="pnl_reserve" runat="server" BackColor="#999999" Height="200px" Width="100%"
                                            Visible="false">
                                            <asp:Label ID="lbl_pnlReserve" runat="server" CssClass="successmsg">Panel: Reserve</asp:Label>
                                        </asp:Panel>
                                        <br />
                                        <br />
                                        <asp:Panel ID="pnl_other" runat="server" BackColor="#0000FF" Height="200px" Width="100%"
                                            Visible="false">
                                            <asp:Label ID="lbl_pnlOther" runat="server" CssClass="successmsg">Panel: Other</asp:Label>
                                        </asp:Panel>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="9" class="centerpanel" style="border-top-style: solid; border-left-style: solid;
                                        border-top-width: 1px; border-left-width: 1px; border-top-color: #E4CB7B; border-left-color: #E4CB7B;">
                                        &nbsp;
                                    </td>
                                </tr>
                    </tr>
                    <!--above this line is table with panels -->

---------------------
My codebehind follows:
---------------------
 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                drpemptype.AppendDataBoundItems = true;
                String strConnString = ConfigurationManager.ConnectionStrings["PersonDataWarehouseConnectionString"].ConnectionString;
                String strQuery = "select PersonTypeID, TypeofPerson from TypeofPerson";
                SqlConnection con = new SqlConnection(strConnString);

                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strQuery;
                cmd.Connection = con;

                try
                {
                    con.Open();
                    drpemptype.DataSource = cmd.ExecuteReader();
                    drpemptype.DataTextField = "TypeofPerson";
                    drpemptype.DataValueField = "PersonTypeID";
                    drpemptype.DataBind();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                    con.Dispose();

                }

                pnl_employee.Visible = false;
                pnl_contractor.Visible = false;
                pnl_reserve.Visible = false;
                pnl_other.Visible = false;
            }

           
        }



 protected void drpemptype_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnl_employee.Visible = true;
        }
------------------------
I was thinking about using nested "if" statements or a "case" statement.

ANY help would be greatly appreciated.

0
Comment
Question by:Codeaddict7423
[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
2 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 2000 total points
ID: 36473688
I'd use a switch statement:

switch drpemptype.SelectedValue:
{
      case "1":
            {
                  panel1.Visible = true;
                  break;
            }
}

Arun
0
 

Author Closing Comment

by:Codeaddict7423
ID: 36474434
Thank You. This worked like a charm.
0

Featured Post

Industry Leaders: 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

609 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