Click here to Skip to main content
15,895,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
How can I add child list items to a parent list item in a dropdown list in ASP.net without using a database?

Thanks in advance!
Posted
Updated 27-Feb-11 22:22pm
v2
Comments
Albin Abel 28-Feb-11 4:20am    
Treeview would be better in this case. There are ways you can display the list hierarchial. But it is not like you select a child and other childs will be shown

You can't have a Parent list item and Child list items in a drop down like this:
Parent1
   Child 1
   Child 2
Parent 2
   Child 3
      Child 4
      Child 5
   Child 6
Drop down just doesn't work like that - it only supports a single level:
Item 1
Item 2
Item 3

Instead, consider a TreeView (if you need a truly hierarchical list) or use two drop downs if you only need one level. When the first changes, it alters the content of the second.
 
Share this answer
 
Comments
arindamrudra 28-Feb-11 4:46am    
But still if he want to display like the first one, then he should have to write a query which returns value like Parent1--Child1.1----Child1.1.1--Child1.2. But you are not using database. If it is coming from XML file then also you can do this.
this[^] might help you.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-Feb-11 4:24am    
I'm not sure why this was downvoted. Compensated 5+
[no name] 28-Feb-11 4:26am    
Thanks manfred. there are lot of univoters are playing around.
arindamrudra 28-Feb-11 4:37am    
I think he was looking for the Griff's answer.
[no name] 28-Feb-11 4:46am    
I too thinks the same.
arindamrudra 28-Feb-11 4:49am    
Please check my comment on Griff's answer.
C#
public partial class Default : System.Web.UI.Page
{
    // First DropdownList items
    protected string[] Movies ={ "select a movie", "Movie_1", "Movie_2", "Movie_3" };
    // Second DropdownList items. One among these to be choosen.
    protected string[] Movie_1={"Theatre_1","Theatre_2","Theatre_3"};
    protected string[] Movie_2 ={ "Theatre_4", "Theatre_5" };
    protected string[] Movie_3 ={ "Theatre_5", "Theatre_6", "Theatre_7" };
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = Movies;
            DropDownList1.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == 1)
        {
            DropDownList2.DataSource = Movie_1;
            DropDownList2.DataBind();
        }
        if (DropDownList1.SelectedIndex == 2)
        {
            DropDownList2.DataSource = Movie_2;
            DropDownList2.DataBind();
        }
        if (DropDownList1.SelectedIndex == 3)
        {
            DropDownList2.DataSource = Movie_3;
            DropDownList2.DataBind();
        }
    }
}
 
Share this answer
 
v2

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