?
Solved

asp.net c# dropdownlist onselectedindexchanged displays specific panels

Posted on 2011-09-02
2
Medium Priority
?
427 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

Independent Software Vendors: 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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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