We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Cascade Dropdownlist not populating

Codeaddict7423
on
Medium Priority
430 Views
Last Modified: 2012-06-27
Hello, I have a page containing four cascading dropdownlist controls, each quering data from a data table.

The first ddl selects a building, the second selects a floor, the third selects a shift and the last selects a position. however, the last ddl does not seem to select the position, based on the previous ddls
My code follows:
--------------------------
ddl_cascade.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ddl_cascade_test01.aspx.cs" Inherits="Detention_DWL.ddl_cascade_test01" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <span style ="font-family:Arial">Select Building : </span>

<asp:DropDownList ID="ddl_Building" runat="server" AutoPostBack = "true" OnSelectedIndexChanged="ddl_Building_SelectedIndexChanged">

<asp:ListItem Text = "--Select Building--" Value = ""></asp:ListItem>

</asp:DropDownList>

 

<br /><br />

<span style ="font-family:Arial">Select Floor : </span>

<asp:DropDownList ID="ddl_floor" runat="server" AutoPostBack = "true" Enabled = "false"  OnSelectedIndexChanged="ddl_floor_SelectedIndexChanged">

<asp:ListItem Text = "--Select Floor--" Value = ""></asp:ListItem>

</asp:DropDownList>

 

<br /><br />

<span style ="font-family:Arial">Select Shift : </span>

 <asp:DropDownList ID="ddl_shift" runat="server" AutoPostBack = "True"
                            Enabled = "False" OnSelectedIndexChanged="ddl_shift_SelectedIndexChanged">
                       <asp:ListItem Text = "--Select Shift--"></asp:ListItem>
</asp:DropDownList>

 <br /><br />
 
 <span style ="font-family:Arial">Select Position : </span>

         <asp:DropDownList ID="ddl_position" runat="server" AutoPostBack = "True" Enabled = "False"
         OnSelectedIndexChanged="ddl_position_SelectedIndexChanged"><asp:ListItem Text = "--Select Position--"></asp:ListItem>
</asp:DropDownList>

 
 
 
 

<br /><br />

<asp:Label ID="lblResults" runat="server" Text="" Font-Names = "Arial" />


   
   
    </div>
    </form>
</body>
</html>
-------------
ddl_cascade.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Detention_DWL
{
    public partial class ddl_cascade_test01 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
         


                ddl_Building.AppendDataBoundItems = true;
                String strConnString = ConfigurationManager.ConnectionStrings["DetentionDWLConnectionString"].ConnectionString;
                String strQuery = "select Building_ID, Building_Name from tbl_Building";
                SqlConnection con = new SqlConnection(strConnString);

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

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

        protected void ddl_Building_SelectedIndexChanged(object sender, EventArgs e)
{
    ddl_floor.Items.Clear();
    ddl_floor.Items.Add(new ListItem("--Select Floor--", ""));
    ddl_floor.AppendDataBoundItems = true;
    String strConnString = ConfigurationManager.ConnectionStrings["DetentionDWLConnectionString"].ConnectionString;
    String strQuery = "select Floor_ID, Floor_Name from tbl_Floor " + "where Building_ID=@Building_ID";
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.Parameters.AddWithValue("@Building_ID", ddl_Building.SelectedItem.Value);  
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = con;
    try
    {
        con.Open();
        ddl_floor.DataSource = cmd.ExecuteReader();
        ddl_floor.DataTextField = "Floor_Name";
        ddl_floor.DataValueField = "Floor_ID";
        ddl_floor.DataBind();
        if (ddl_floor.Items.Count > 1)
        {
            ddl_floor.Enabled = true;
        }
        else
        {
            ddl_floor.Enabled = false;
            ddl_shift.Enabled = false;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
}

        protected void ddl_floor_SelectedIndexChanged(object sender, EventArgs e)
{
    ddl_shift.Items.Clear();
    ddl_shift.Items.Add(new ListItem("--Select Shift--", ""));
    ddl_shift.AppendDataBoundItems = true;
    String strConnString = ConfigurationManager.ConnectionStrings["DetentionDWLConnectionString"].ConnectionString;
    String strQuery = "select Shift_ID, Shift_Name from tbl_Shift " + "where Floor_ID=@Floor_ID";
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.Parameters.AddWithValue("@Floor_ID", ddl_floor.SelectedItem.Value);
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = con;
    try
    {
        con.Open();
        ddl_shift.DataSource = cmd.ExecuteReader();
        ddl_shift.DataTextField = "Shift_Name";
        ddl_shift.DataValueField = "Shift_ID";
        ddl_shift.DataBind();
        if (ddl_shift.Items.Count > 1)

        {
            ddl_shift.Enabled = true;
        }
        else
        {
            ddl_shift.Enabled = false;
        }  

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
      con.Close();
      con.Dispose();
    }
}

        protected void ddl_shift_SelectedIndexChanged(object sender, EventArgs e)
{
    ddl_position.Items.Clear();
    ddl_position.Items.Add(new ListItem("--Select Position--", ""));
    ddl_position.AppendDataBoundItems = true;
    String strConnString = ConfigurationManager.ConnectionStrings["DetentionDWLConnectionString"].ConnectionString;
    String strQuery = "select POS_ID, POS_Name from tbl_Position " + "where Shift_ID=@Shift_ID";
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.Parameters.AddWithValue("@Shift_ID", ddl_shift.SelectedItem.Value);
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = con;
    try
    {
        con.Open();
        ddl_position.DataSource = cmd.ExecuteReader();
        ddl_position.DataTextField = "POS_Name";
        ddl_position.DataValueField = "POS_ID";
        ddl_position.DataBind();
        if (ddl_position.Items.Count > 1)
        {
            ddl_position.Enabled = true;
        }
        else
        {
            ddl_position.Enabled = false;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
       con.Close();
       con.Dispose();
    }

    lblResults.Text = "Your Choices Are: " +

                      ddl_Building.SelectedItem.Text + " -----> " +
                      ddl_floor.SelectedItem.Text + " -----> " +
                      ddl_shift.SelectedItem.Text + " -----> " +
                      ddl_position.SelectedItem.Text ;
}

        protected void ddl_position_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       


    }
}
---------

ANY help would be greatly appreciated
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.