2

In my ASP.NET folder i have 3 dropdownboxes who are filled with sorts of categories with 3 different SQLdatascources. Each dropdownlist depends on the one above it.
The intention of this thing is that after selecting something in dropdownlist1, the datasource in dropdownlist2 changes. So the data in dropdown2 depend on the selected value in dropdown1. And the data in dropdown3 depend on the selected value in dropdown2.
It all works nice and dandy a first time. But when I selected a value in dd1 and after this a value in dd2, it starts to fail.
When I change the value a second time in dropdown1 for example, the other dropdownlists don't alter.

Thx in advance for replies

3 Answers 3

4

To do this kind of thing, you need to make sure your top level drop down list is only populated once, so put it in the page load with a !isPostback around it. Then attach a OnSelectedIndexChanged() event on that top level dropdownlist, and in it, be sure to clear the items in the second level dropdown list before setting it's new datasource.

Then a OnSelectedIndexChanged() on the second level dropdown list, and be sure to clear the third before populating it.

Then the third doesn't need any events.

Sign up to request clarification or add additional context in comments.

4 Comments

thx m8 it already helped a bunch ^^ now there is just another problem when i reselect something a second time, it adds the the new items to the previous items. Is there a way to completely clear the dropdownlist(except for the first item that has value 0 with text "make choice here")?
Cool, I've done that scenario so many times, but I remember it being tricky the first few times
I'd clear the entire dropdownlist with a ddlMyDropDownList.Items.Clear(), then ddlMyDropDownList.Items.Add(New ListItem("0", "Make a Choice"))
I just wanted to mention (for the future readers with the same problem) that I used ddList1.Items.Insert(0, new ListItem("0", "Make a Choice")); in stead of the **.Add(new ListItem("0", "Make a Choice")) I do this because I want to be able to choose at what position the new listitem is added. If you use Add() instead, it just pushes the new Item at the bottom of the list.
0

Put the code to fill dd2 in the OnSelectedIndexChanged of dd1, and do the same for the OnSelectedIndexChanged of dd2 (to fill dd3).

Comments

0

Example of code with 2 different tables connected by keys, in this example: companyID.

public partial class Default : Page
{
    public void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var datacontext = new DataClasses1DataContext();
            List<Company> companies = datacontext.Companies.ToList();
            DropDownList1.DataSource = companies;
            DropDownList1.DataValueField = "CompanyID";
            DropDownList1.DataTextField = "CompanyName";
            DropDownList1.DataBind();
        }
    }

    public void DropDownList1SelectedIndexChanged(object sender, EventArgs e)
    {
        using (var dc = new DataClasses1DataContext())
        {
            DetailsView2.DataSource = null;
            DetailsView2.DataBind();

            DetailsView1.DataSource = dc.Companies.Where(d => d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();
            DetailsView1.DataBind();

            List<Contact> contacts = 
            dc.Contacts.Where(d => d.CompanyID.Equals(DropDownList1.SelectedValue)).ToList();            
            DropDownList2.DataSource = contacts;
            DropDownList2.DataTextField = "LastName";
            DropDownList2.DataValueField = "ContactID";
            DropDownList2.DataBind();

            if (contacts.Any())
            {
                DetailsView2.DataSource = dc.Contacts.Where(c => c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
                DetailsView2.DataBind();
            }

        }
    }

    public void DropDownList2SelectedIndexChanged(object sender, EventArgs e)
    {
        using (var dc = new DataClasses1DataContext())
        {
            DetailsView2.DataSource = dc.Contacts.Where(c => c.ContactID.Equals(DropDownList2.SelectedValue)).ToList();
            DetailsView2.DataBind();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.