[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cascade Dropdownlist not populating

Posted on 2011-04-25
1
Medium Priority
?
422 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
0
Comment
Question by:Codeaddict7423
1 Comment
 
LVL 21

Accepted Solution

by:
Alfred A. earned 2000 total points
ID: 35497295
Looking at your code dump, it looks like everything is OK.  Do you get any list of items under the position dropdownlist?  Can you provide a scenario where selecting a building, then floor, then shift, causes the position items to be wrong?  What do you currently get right now?  Do you get all position items or none at all?

Have you as well, double check your query related to extracting position items?  Is your SQL query "select POS_ID, POS_Name from tbl_Position " + "where Shift_ID=@Shift_ID" providing the result correctly when tested in query analyzer?  For example, testing known Shift_ID numbers and determining values for POS_ID and POS_Name.
0

Featured Post

Industry Leaders: 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

873 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