Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 4 drop dwon lists in dxe:ASPxComboBox. when i click 2nd dropdown with out selecting 1st dropdown it will display as plase select 1st drop down.how can i write the logic please tell me

What I have tried:
Posted
Updated 5-Oct-16 23:58pm
Comments
Suvendu Shekhar Giri 6-Oct-16 6:53am    
What have you tried so far?

1 solution

Hello

If you mean you are looking for Cascade DropDownList, the code is :

CodeBehind:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            gvProduct.DataSourceID = ldsProduct.ID;
            if (!IsPostBack)
            {
                ddlType.AppendDataBoundItems = true;

                string strConnection = "Data Source =Ali-HP;Database=EzBuy;Integrated Security=yes";
                using (SqlConnection Connection = new SqlConnection(strConnection))
                {

                    String strQuery = "select TypeID, TypeName from PrType";
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = strQuery;
                    cmd.Connection = Connection;

                    try
                    {
                        cmd.Connection.Open();
                        ddlType.DataSource = cmd.ExecuteReader();
                        ddlType.DataTextField = "TypeName";
                        ddlType.DataValueField = "TypeID";
                        ddlType.DataBind();
                    }

                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    finally
                    {
                        cmd.Connection.Close();
                        cmd.Connection.Dispose();
                    }
                }
            }
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {

            ddlSubType.Items.Clear();
            ddlSubType.Items.Add(new ListItem("select Product", ""));
            ddlCompany.Items.Clear();
            ddlCompany.Items.Add(new ListItem("select company", ""));

            ddlSubType.AppendDataBoundItems = true;

            string strConnection = "Data Source =Ali-HP;Database=EzBuy;Integrated Security=yes";
            using (SqlConnection Connection = new SqlConnection(strConnection))
            {

                String strQuery = "select SubTypeID, SubTypeName from PrSubType where TypeID=@TypeID ";
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.AddWithValue("@TypeID", ddlType.SelectedItem.Value);
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strQuery;
                cmd.Connection = Connection;

                try
                {

                   cmd.Connection.Open();
                   ddlSubType.DataSource = cmd.ExecuteReader();
                   ddlSubType.DataTextField = "SubTypeName";
                   ddlSubType.DataValueField = "SubTypeID";
                   ddlSubType.DataBind();

                   if (ddlSubType.Items.Count > 1)
                   {
                       ddlSubType.Enabled = true;
                   }

                   else
                   {
                    ddlSubType.Enabled = false;
                    ddlCompany.Enabled = false;
                   }

               }

               catch (Exception ex)
               {
                throw ex;
               }

               finally
               {
                cmd.Connection.Close();
                cmd.Connection.Dispose();
               }

            }
        }

        protected void ddlSubType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlOS.Items.Clear();

            ddlOS.Items.Add(new ListItem("select os", ""));

            ddlOS.AppendDataBoundItems = true;

            string strConnection = "Data Source =Ali-HP;Database=EzBuy;Integrated Security=yes";
            using (SqlConnection Connection = new SqlConnection(strConnection))
            {

                String strQuery = "select OSID, OSName from PrOS where TypeID=@TypeID ";
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.AddWithValue("@TypeID", ddlType.SelectedItem.Value);

                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strQuery;
                cmd.Connection = Connection;

                try
                {

                    cmd.Connection.Open();
                    ddlOS.DataSource = cmd.ExecuteReader();
                    ddlOS.DataTextField = "OSName";
                    ddlOS.DataValueField = "OSID";
                    ddlOS.DataBind();

                    if (ddlOS.Items.Count > 1)
                    {
                        ddlOS.Enabled = true;
                    }

                    else
                    {
                        ddlOS.Enabled = false;
                    }
                }

                catch (Exception ex)
                {
                    throw ex;
                }

                finally
                {
                    cmd.Connection.Close();
                    cmd.Connection.Dispose();
                }
            }
        }
        protected void ddlOS_SelectedIndexChanged(object sender, EventArgs e)
        {

            ddlCompany.Items.Clear();

            ddlCompany.Items.Add(new ListItem("select company", ""));

            ddlCompany.AppendDataBoundItems = true;

            string strConnection = "Data Source =Ali-HP;Database=EzBuy;Integrated Security=yes";
            using (SqlConnection Connection = new SqlConnection(strConnection))
            {
               
                String strQuery = "select CompanyID, CompanyName from PrCompany where TypeID=@TypeID ";
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.AddWithValue("@TypeID", ddlType.SelectedItem.Value);

                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strQuery;
                cmd.Connection = Connection;

                try
                {

                   cmd.Connection.Open();
                   ddlCompany.DataSource = cmd.ExecuteReader();
                   ddlCompany.DataTextField = "CompanyName";
                   ddlCompany.DataValueField = "CompanyID";
                   ddlCompany.DataBind();

                   if (ddlCompany.Items.Count > 1)
                   {
                      ddlCompany.Enabled = true;
                   }

                   else
                   {
                      ddlCompany.Enabled = false;
                   }
               }

               catch (Exception ex)
               {
                   throw ex;
               }

               finally
               {
                cmd.Connection.Close();
                cmd.Connection.Dispose();
              }
           }
        }


        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


description: when you want to select 2nd DDL, you have to set a value for the 1st one first. the 2nd DDL is disabled until you have selected the value for 1st one.

hope it solve your problem
best regards
 
Share this answer
 
Comments
Suvendu Shekhar Giri 6-Oct-16 6:52am    
From where you found all these code?
OP hasn't shared anything?
Ali Majed HA 6-Oct-16 6:56am    
I have write them with the help of "StackOverFlow". It has been working in my project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900