Solved

asp.net c# dropdownlist onselectedindexchanged displays specific panels

Posted on 2011-09-02
2
422 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

17 Experts available now in Live!

Get 1:1 Help Now