Click here to Skip to main content
15,923,164 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two drop down lists as DDL1 & DDL2. I have 3 items in DDL1, where the 1st item is --SELECT--. When I select the 22nd item in DDL1, the items of DDL2 gets populated from the database. But the problem is, when I select the --SELECT-- item in DDL1, the item which was previously selected in DDL2 remains as it is and does not removes.I want that when ever i select --SELECT-- in DDL1, DDL2 should get empty & when I select 1st or 2nd item from DDL1, then DDL2 should get populated from the database. Can any one please help ?
Posted
Comments
Kornfeld Eliyahu Peter 10-Feb-14 2:55am    
Have you done anything so far? Show some effort (code or searching)! As is it ain't a question...maybe a homework one...
Member 10520129 10-Feb-14 3:15am    
I am posting a portion of my coding..

public void FillActivityWaterStorage()
{
try
{
dt = Query.Guindy_ActivityWaterStorage_Population();
if (dt.Rows.Count > 0)
{
ddl_ActivityWaterStorage.Items.Clear();
ddl_ActivityWaterStorage.DataTextField = "ActivityName";
ddl_ActivityWaterStorage.DataValueField = "ActivityID";
ddl_ActivityWaterStorage.DataSource = dt;
ddl_ActivityWaterStorage.DataBind();
ddl_ActivityWaterStorage.Items.Insert(0, new ListItem("--SELECT--"));
}
else
{
ddl_SubActivityWaterStorage.Items.Insert(0, new ListItem("--SELECT--"));
}

}
catch
{
}
}

this is your previous code:

public void FillActivityWaterStorage() 
{ 
  try 
    {  
    dt = Query.Guindy_ActivityWaterStorage_Population(); 
      if (dt.Rows.Count > 0) 
             {
               ddl_ActivityWaterStorage.Items.Clear(); 
               ddl_ActivityWaterStorage.DataTextField = "ActivityName";      
               ddl_ActivityWaterStorage.DataValueField = "ActivityID";  

               ddl_ActivityWaterStorage.DataSource = dt; 
               ddl_ActivityWaterStorage.DataBind();
               ddl_ActivityWaterStorage.Items.Insert(0, new ListItem("--SELECT--")); 
             } 
       else 
             { ddl_SubActivityWaterStorage.Items.Insert(0, new ListItem("--SELECT--"));
             } 
       } 
    catch { } 
 }


you do work around here. . . add item in your DropDownList2 jsut like you did in dropdownList1

ddl.Items.Insert(0,"--");//just like this(optional if the code below dont work) 

now for the ddl selected changed:

public void ddl_ActivityWaterStorage_SelectedChanged(Sender object,EventArguments e)
{
 if(ddl_ActivityWaterStorage.SelectedValue==The2nditem)
   {
    ddl_SubActivityWaterStorage.DataSource=PopulateDataFromDatabase;
    ddl_SubActivityWaterStorage.DataBind();
   }
 else
  {
   ddl_SubActivityWaterStorage.DataSource=null;
   ddl_SubActivityWaterStorage.DataBind();
  }

}

hope it will work,comment if got error or something wrong so I can check the code again :)
 
Share this answer
 
v2
Comments
Member 10520129 10-Feb-14 3:50am    
its just working fine
The14thNoah 10-Feb-14 22:38pm    
I hope it will help to your problem :)
You need to do it on SelectedIndexChanged Event.
C#
protected void ddlSparte_SelectedIndexChanged(object sender, EventArgs e)
    {
 DataRow dr = dt.NewRow();
            dr["Produktname"] = "...SELECT...";
            dr["Produkt_id"] = "0";
            dt.Rows.InsertAt(dr, 0);

            ddlProdukt.DataSource = dt;
            ddlProdukt.DataTextField = "Produktname";
            ddlProdukt.DataValueField = "Produkt_id";
            ddlProdukt.DataBind();

}


Please change your dropdownlist and others. Its just a concept.
 
Share this answer
 
Please try This...


link1
 
Share this answer
 

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