0

How would I set up a cascading dropdown in my custom web part's editor part so that when I, for instance, choose a list from the first dropdown the second dropdown would be populated with a list of the columns for this chosen list.

I've set up my first dropdown with AutoPostBack to be true so that the page posts back when it's value is changed but I can't figure out how to get the value from that dropdown.

1 Answer 1

1

It looks like you are using Server-side object model via C# at custom visual web part.

So Consider you have drop-down list called DropDownList1at your custom visual web part , click on this control in design view to open code view with this event DropDownList1_SelectedIndexChanged within this event you can get the selected value as the following :

 string selecteditem = DropDownList1.SelectedItem.ToString();

Additionally, To fill the second dropdown list , at the same event DropDownList1_SelectedIndexChanged try to add this code based on your entries as the following

using (SPSite site = new SPSite(http://siteurl))
 {
   using (SPWeb web = site.OpenWeb())
    {

     string selecteditem = DropDownList1.SelectedItem.ToString();
     SPList myListe = web.Lists.["Categorie"];
     SPQuery query = new SPQuery();
     query.Query = "<Where><Eq><FieldRef Name='Title'/>" + "<Value Type='Text'>" + selecteditem  + "</Value></Eq></Where>";
                        SPListItemCollection items = myListe.GetItems(query);
                        foreach (SPListItem item in items)
                        {
                         DropDownList2.Items.Add(item ["Title"].ToString())
                        }
       }
  }

Note :

  • I advise adding update panel to hold the 2 drop down lists to avoid postback and page load every time you select an item from the first drop down list

  • At this code query.Query build your where condition based on your requirement

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.