Solved

asp.net c# dropdownlist onselectedindexchanged displays specific panels

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Technology Resume 7 71
VS2010 Build fails to install 14 82
ASP.NET Built-In Report Creator / Viewer 5 25
Send parameters via datatable ajax call 4 12
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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