0

I have 3 dropdownlists and I already set dropdownlist 2 based on SelectedValue of dropdownlist 1, but my dropdownlist 3 has no response based on selection in dropdownlist2

if(DropDownList1.SelectedValue =="1")
{
    DropDownList2.Items.Clear();
    DropDownList2.Items.Insert(0, new ListItem("A", ""));
    DropDownList2.Items.Insert(1, new ListItem("B", ""));
}

This is how I add items in Dropdownlist2 based on selection in Dropdownlist1 but it does not work in the third dropdownlist whic does not have any item after I select A in Dropdownlist2:

if (DropDownList2.SelectedValue == "0")
{
    DropDownList3.Items.Clear();
    DropDownList3.Items.Insert(0, new ListItem("A-1", ""));
}
4
  • are you able to debug the code? Does is execute the code of populating DropDownList3 ? When these two codes are executed? Commented Oct 7, 2019 at 3:20
  • it should be selectedIndex not selectedvalue right? added an answer and explanation Commented Oct 7, 2019 at 3:27
  • @ChetanRanpariya the only problem here is the dropdownlist 3 does not have any item after i selected something in dropdownlist 2 Commented Oct 7, 2019 at 3:30
  • If you debug the code, you can figure out why it is not adding any items in DropDownList3. @HollowLooi Commented Oct 7, 2019 at 3:34

1 Answer 1

1

i think you got the selectedvalue wrong, i think it should be selectedindex

if (DropDownList2.SelectedIndex == 0)
{
    DropDownList3.Items.Clear();
    DropDownList3.Items.Insert(0, new ListItem("A-1", ""));
}

if you will still want to use selectedvalue change the bindings of new ListItem("A", "") to new ListItem("A", "");

//populate first ddl with corret item and value
if(DropDownList1.SelectedValue =="1")
{
            DropDownList2.Items.Clear();
            DropDownList2.Items.Insert(0, new ListItem("A", ""));
            DropDownList2.Items.Insert(1, new ListItem("B", ""));
}

if (DropDownList2.SelectedValue == "A")
{
    DropDownList3.Items.Clear();
    DropDownList3.Items.Insert(0, new ListItem("A-1", ""));
}

DropdownList.Items.Insert implements ddl.Items.Insert(indexPosition, new ListItem("displayItem", "value");

so since you are searching for "0" in the selectedvalue which in your example does not exists since new ListItem("A", "") it will not execute the statement inside

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

3 Comments

I changed into index, but the third dropdownlist still show blank for me
@HollowLooi can you show your code for the implementation of the dropdownlist?
Do you mean the whole function or ?

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.