Solved

asp.net c# dropdownlist onselectedindexchanged displays specific panels

Posted on 2011-09-02
2
423 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
2 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

16 Experts available now in Live!

Get 1:1 Help Now