Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Suppose I have three DropDownLists each with the items: A, B,C,D and E.these are retriving from table data. If I selected item D from the DropDownList 2, then DropDownList1 and 3 will show the items A, B, C and E only, since my code works to remove / disable the selected item from the other DropDownLists.and i want to add Select option for all dropdownlist, if i click on that it wont go 'select' option. Any idea? Please help.

Thank You
Posted
Updated 22-Dec-14 20:08pm
v4
Comments
Anisuzzaman Sumon 23-Dec-14 0:38am    
This is not a good question however give some part of your code .
syed shanu 23-Dec-14 1:20am    
But why do you have same data in 3 dropdownlist

1 solution

Check below peace of code. It might help you.

Let's say we have 3 dorpdown.
XML
<asp:DropDownList ID="DropDownList1" runat="server" Width="150px"
            AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>a</asp:ListItem>
            <asp:ListItem>b</asp:ListItem>
            <asp:ListItem>c</asp:ListItem>
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem></asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:DropDownList ID="DropDownList2" runat="server" Width="150px"
            AutoPostBack="True" onselectedindexchanged="DropDownList2_SelectedIndexChanged">
            <asp:ListItem>a</asp:ListItem>
            <asp:ListItem>b</asp:ListItem>
            <asp:ListItem>c</asp:ListItem>
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem></asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:DropDownList ID="DropDownList3" runat="server" Width="150px"
            AutoPostBack="True" onselectedindexchanged="DropDownList3_SelectedIndexChanged">
            <asp:ListItem>a</asp:ListItem>
            <asp:ListItem>b</asp:ListItem>
            <asp:ListItem>c</asp:ListItem>
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem></asp:ListItem>
        </asp:DropDownList>


Below is the code behind code.
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string item = DropDownList1.Text;
            DropDownList2.Items.Remove(item);
            DropDownList3.Items.Remove(item);
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string item = DropDownList2.Text;
            DropDownList1.Items.Remove(item);
            DropDownList3.Items.Remove(item);
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string item = DropDownList3.Text;
            //here you can bind the Dropdown and then remove the item. Do this wherever you require to do
            DropDownList1.Items.Remove(item);
            DropDownList2.Items.Remove(item);
        }


If you want to do something similar on Page Load you can do that also based on above code.
 
Share this answer
 
v2
Comments
sharat naik 23-Dec-14 2:04am    
article was good.. here dropdown bind with the table data.. if i select 'SELECT' option, that 'SELECT' option removes in the other dropdown..
my code is below piz check it

public void D1()
{
SqlDataAdapter da = new SqlDataAdapter(" SELECT Distinct [Drum_No] FROM [DrumMaster] where grade='Recovery Grade'", connection);
DataTable dt = new DataTable();
da.Fill(dt);
ddl_D1.DataSource = dt;
ddl_D1.DataTextField = "Drum_No";
ddl_D1.DataValueField = "Drum_No";
ddl_D1.DataBind();
ddl_D1.Items.Insert(0, "Select");
}
Praveen Kumar Upadhyay 23-Dec-14 2:08am    
Okay then before removing the item from Dropdown, you need to bind the Dropdown.

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