Creating Dependent DropDownLists...

Forcing the contents of one DropDownList to change based upon the selection of a value from another DropDownList.
How can do so in C#(Asp.net)

i have kept a dropdownlsits for locations --->
Droploc1 ---of locations
and acording to this i have to bind datas to my second dropdwnlsts
Droplab--which contains labs according to a particular locations

Hope u all have understood this,i will give my code here but its not working!!!

Droplab.Items.Clear();
			SqlConnection cone1 = new SqlConnection(@"Server=sqloledb.1;User ID=sa;password=;Data Source=IRID_BDC1;Initial Catalog=hello");
            string searchVal1=Droploc1.SelectedItem.Text;
			Labwelc.Text=searchVal1;
			string selectSQL1 = "SELECT Lab from Loc_det where Lab='"+searchVal1+"'";
			//string searchVal1=Droploc1.SelectedItem.Text;
			//Response.Write(Droploc1.SelectedItem.Text);
			SqlCommand cmdd = new SqlCommand(selectSQL1, cone1);
			SqlDataReader reader1;
			try
			{
				cone1.Open();
				reader1 = cmdd.ExecuteReader();
				while(reader1.Read())
				{
					ListItem newItem = new ListItem();
					newItem.Text = reader1["Lab"].ToString();
					//newItem.Value = reader1["Slno"].ToString();
					Droplab.Items.Add(newItem);
				}
				reader1.Close();
			}
			catch (Exception err)
			{
				Labwelc.Text = "Error reading list of lab. ";
				Labwelc.Text += err.Message;
			}
			finally
			{
				cone1.Close();
			}

hi you need to set the autopostback properties of all dropdownlists to "TRUE" and then you need to write the code in the select-indexchanged event.

hi you need to set the autopostback properties of all dropdownlists to "TRUE" and then you need to write the code in the select-indexchanged event.

hi kameshwari,
thank u....but i have set the autopostback properties of all dropdownlists to "TRUE" and writen the code in the select-indexchanged event.
But ten too,,...its no working,if i select the first one....the page refreshes and all the other controls visibility will be false then.


....IChu

hi ichu,
i have created 2 tables in database. Those are

Locations:
lid -(int)
location - varchar

Labs:
labid - (int)
lid - (int)
labname -(varchar)

In the designview i have taken two dropdownlists namely dropdownlist1 and dropdownlist2. In dropdownlist1 i am binding the location names form the TABLE LOCATIONS ->location. Okay??

Here is the code....

codebehind:

using System.Data.SqlClient;



public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection(@"Data Source=WINKC185045-MAB\SQLEXPRESS;Initial Catalog=Records;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            SqlDataAdapter da = new SqlDataAdapter("select distinct location,lid from locations", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "locations");
            DropDownList1.Items.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                DropDownList1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
                DropDownList1.Items[i].Value = ds.Tables[0].Rows[i][1].ToString();
            }
        }

              
    }
   
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataAdapter da2 = new SqlDataAdapter("select labname from labs where lid ='" + DropDownList1.SelectedValue + "'", con);
        DataSet ds2 = new DataSet();
        da2.Fill(ds2, "labs");
        DropDownList2.Items.Clear();
        for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
        {
            DropDownList2.Items.Add(ds2.Tables[0].Rows[i][0].ToString());
        }


    }
}

Source code:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        &nbsp; &nbsp;
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RecordsConnectionString %>"
            SelectCommand="SELECT * FROM [table1]"></asp:SqlDataSource><asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
            </asp:DropDownList></div>
    </form>
</body>

Hope it solves ur problem. Let me know if u still facing problem with this code.

All the best...

hi ichu,
i have created 2 tables in database. Those are

Locations:
lid -(int)
location - varchar

Labs:
labid - (int)
lid - (int)
labname -(varchar)

In the designview i have taken two dropdownlists namely dropdownlist1 and dropdownlist2. In dropdownlist1 i am binding the location names form the TABLE LOCATIONS ->location. Okay??

Here is the code....

codebehind:

using System.Data.SqlClient;



public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection(@"Data Source=WINKC185045-MAB\SQLEXPRESS;Initial Catalog=Records;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            SqlDataAdapter da = new SqlDataAdapter("select distinct location,lid from locations", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "locations");
            DropDownList1.Items.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                DropDownList1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
                DropDownList1.Items[i].Value = ds.Tables[0].Rows[i][1].ToString();
            }
        }

              
    }
   
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataAdapter da2 = new SqlDataAdapter("select labname from labs where lid ='" + DropDownList1.SelectedValue + "'", con);
        DataSet ds2 = new DataSet();
        da2.Fill(ds2, "labs");
        DropDownList2.Items.Clear();
        for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
        {
            DropDownList2.Items.Add(ds2.Tables[0].Rows[i][0].ToString());
        }


    }
}

Source code:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        &nbsp; &nbsp;
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RecordsConnectionString %>"
            SelectCommand="SELECT * FROM [table1]"></asp:SqlDataSource><asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
            </asp:DropDownList></div>
    </form>
</body>

Hope it solves ur problem. Let me know if u still facing problem with this code.

All the best...

data is not binding into dropdownlist......

its completely left blank.................

hav u created the tables as mentioned above??

code for sql: Locations table

USE [Records]
GO
/****** Object:  Table [dbo].[locations]    Script Date: 08/11/2009 16:04:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[locations](
	[location] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[lid] [int] NULL
) ON [PRIMARY]

code for LABS table:

USE [Records]
GO
/****** Object:  Table [dbo].[labs]    Script Date: 08/11/2009 16:05:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[labs](
	[lid] [int] NULL,
	[labname] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[labid] [int] NULL
) ON [PRIMARY]

THE above code is working in my system... try by creating like above...

hav u created the tables as mentioned above??

code for sql: Locations table

USE [Records]
GO
/****** Object:  Table [dbo].[locations]    Script Date: 08/11/2009 16:04:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[locations](
	[location] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[lid] [int] NULL
) ON [PRIMARY]

code for LABS table:

USE [Records]
GO
/****** Object:  Table [dbo].[labs]    Script Date: 08/11/2009 16:05:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[labs](
	[lid] [int] NULL,
	[labname] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[labid] [int] NULL
) ON [PRIMARY]

THE above code is working in my system... try by creating like above...

My tables--sql serverdb

Loc_mast(table1)

Slno int (Slno(genertes automatically)(PK)
Loc(PK) navarchar2


Loc_det(table2)

Slno int (Slno(genertes automatically)(PK)
Loc navarchar2 Location names(PK)
Lab navarchar2 Lab names
Tot_sys int Total systems in each labs


........ichu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.