Click here to Skip to main content
12,451,243 members (53,290 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
How to fill a second dropdownlist based on the selection made in the first one.
i want to use a condition:
for example:
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
       {
 
           if (dropdownlis1t.SelectedValue == "1")
           {
               SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
               con.Open();
 
               SqlCommand cmd = new SqlCommand("Select [Code] from Table where Index= '3' OR Index= '2'", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               con.Close();
               dropdownlist2.DataSource = ds;
               dropdownlist2.DataTextField = "Code";
               dropdownlist2.DataValueField = "Code";
               dropdownlist2.DataBind();
 
           }
           else
               if (dropdownlist1.SelectedValue == "2")
               {
 
                   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
                   con.Open();
 
                     SqlCommand cmd = new SqlCommand("Select [Code] from Table where Index= '1'", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               con.Close();
               dropdownlist2.DataSource = ds;
               dropdownlist2.DataTextField = "Code";
               dropdownlist2.DataValueField = "Code";
               dropdownlist2.DataBind();
               }
Posted 18-Jan-13 10:16am
Comments
richcb 18-Jan-13 16:19pm
   
Does your example not work?
Annnaa 18-Jan-13 16:31pm
   
noo
the second dropdownlist doesnt take any value
remains empty
richcb 18-Jan-13 16:48pm
   
In your sqlcommand, you need to remove the single tick marks from the numbers in the where clause. Your dropdownlist2 was not getting any data because your query is bad is my guess. Try that and let me know. "Select [code] from table where Index = 1;"
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

according to your code..the values does not populate to the dropdownlist.

dropdownlist2.DataTextField = "Code";
                dropdownlist2.DataValueField = "Code";

because, it will not hold the values but it just assigns the values to the
dropdownlist textfield and value field.

to populate.this is one of the method

prepare the dataset;
dataset dv;
int n = dv.Tables["tblcountry"].Rows.Count;
 
     DropDownList.Items.Clear();
     DropDownList.Items.Add("--Select--");
 
     for (int i = 0; i <= n - 1; i++)
     {
         DropDownList.DataSource = dv;
         ListItem li = new ListItem();
         li.Text = dv.Tables["tblcountry"].Rows[i][1].ToString();
         li.Value = dv.Tables["tblcountry"].Rows[i][0].ToString();
         DropDownList.Items.Add(li);
     }
it works try this method.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I think you need to perform a callback whenever, the first drop down changes. It's being a long time I wrote code to do that. This link could be helpful.
http://support.microsoft.com/kb/910450[^]
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160826.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100