Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" Width="150px" style="padding:0" Visible="True">
        </asp:DropDownList><asp:DropDownList ID="DropDownList2" runat="server" Width="150px"  style="padding:0" Visible="True">
        </asp:DropDownList><asp:DropDownList ID="DropDownList3" runat="server" Width="150px"  style="padding:0" Visible="True">
        </asp:DropDownList>


My Code to add items to the dropdownlists are below. However, i wanted to know how i can make sure when one item from below dropdown is selected it dissappears from the other drop downs. I want to do this without a button. what kind of event will i have to use? THANK YOU SO MUCH IN ADVANCE!

C#
DropDownList1.Items.Add("")
DropDownList1.Items.Add("Test1")
DropDownList1.Items.Add("Test2")
DropDownList1.Items.Add("Test3")
DropDownList1.Items.Add("Test4")
DropDownList2.Items.Add("")
DropDownList2.Items.Add("Test1")
DropDownList2.Items.Add("Test2")
DropDownList2.Items.Add("Test3")
DropDownList2.Items.Add("Test4")
DropDownList3.Items.Add("")
DropDownList3.Items.Add("Test1")
DropDownList3.Items.Add("Test2")
DropDownList3.Items.Add("Test3")
DropDownList3.Items.Add("Test4")
DropDownList4.Items.Add("")
DropDownList4.Items.Add("Test1")
DropDownList4.Items.Add("Test2")
DropDownList4.Items.Add("Test3")
DropDownList4.Items.Add("Test4")


Therefore, if "Test1" is selected from dropdownlist1. i want it to dissappear on the other dropdowns!
Thank You

What I have tried:

Pre-render didn't work :( Not sure what to do
Posted
Updated 4-Nov-16 19:05pm
Comments
j snooze 4-Nov-16 15:39pm    
This should do the trick(syntax may not be spot on). Use it on the selected index change event of a drop down.

DropDownListX.Items.Remove(DropDownListX.Items.FindByValue("Test1"));

DropDownListX.Items.Remove(DropDownListX.Items.FindByValue(DropDownListX.SelectedItem.Text));
JT1992 4-Nov-16 15:41pm    
where can i add this in as an event? i have no buttons to do this. and it needs to happen dynamically.
JT1992 7-Nov-16 14:27pm    
What would i do if the user selects item Test1 and then unselects item Test1. how can i add it back into the list for the other drop downs if the user unselects item Test1 in dropdownlist1?
JT1992 9-Nov-16 10:26am    
aything???
JT1992 9-Nov-16 10:26am    
can you check my comment please thanks

1 solution

Here is a sample code for your requirement, you can modify it as per your requirement -

ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" Width="150px" AutoPostBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>

        <asp:DropDownList ID="DropDownList2" runat="server" Width="150px" >
        </asp:DropDownList>


on page load -

C#
if (!IsPostBack)
       {
           DropDownList1.Items.Add("");
           DropDownList1.Items.Add("Test1");
           DropDownList1.Items.Add("Test2");
           DropDownList1.Items.Add("Test3");
           DropDownList1.Items.Add("Test4");
           DropDownList2.Items.Add("");
           DropDownList2.Items.Add("Test1");
           DropDownList2.Items.Add("Test2");
           DropDownList2.Items.Add("Test3");
           DropDownList2.Items.Add("Test4");
      }


C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
               DropDownList2.Items.Remove(DropDownList2.Items.FindByValue(DropDownList1.SelectedValue));
    }
 
Share this answer
 
Comments
JT1992 7-Nov-16 9:25am    
What would i do if the user selects item Test1 and then unselects item Test1. how can i add it back into the list for the other drop downs if the user unselects item Test1 in dropdownlist1?
JT1992 9-Nov-16 10:26am    
can you check my comment please thanks
pparya27 9-Nov-16 12:10pm    
friend use some conditions (if else) inside the DropDownList1_SelectedIndexChanged event. and bind back the items in second dropdownlist.

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