Disable selected DropDownList Item (Option) in others using C# and VB.Net in ASP.Net

Last Reply 14 days ago By dharmendr

Posted 14 days ago

Hey,

I want to bind three dropdownlist with one database and when I select any value from dropdown1, that value should be disabled from dropdown2 and dropdown3. when I select value from dropdown2 it should be disabled from other dropdownlists.

so, please suggest me something.

Posted 14 days ago

Hi nid@patel,

Check this example. Now please take its reference and correct your code.

Here i have binded the DropDown using DataTable. You need to bind it from Database.

HTML

<asp:DropDownList runat="server" ID="ddlFruits1" AutoPostBack="true" Width="150px"
    OnSelectedIndexChanged="SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlFruits2" AutoPostBack="true" Width="150px"
    OnSelectedIndexChanged="SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlFruits3" AutoPostBack="true" Width="150px"
    OnSelectedIndexChanged="SelectedIndexChanged">
</asp:DropDownList>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        System.Data.DataTable dt = GetFruits();
        ddlFruits1.DataSource = dt;
        ddlFruits1.DataTextField = "Name";
        ddlFruits1.DataValueField = "Id";
        ddlFruits1.DataBind();
        ddlFruits1.Items.Insert(0, new ListItem("Select", "0"));

        ddlFruits2.DataSource = dt;
        ddlFruits2.DataTextField = "Name";
        ddlFruits2.DataValueField = "Id";
        ddlFruits2.DataBind();
        ddlFruits2.Items.Insert(0, new ListItem("Select", "0"));

        ddlFruits3.DataSource = dt;
        ddlFruits3.DataTextField = "Name";
        ddlFruits3.DataValueField = "Id";
        ddlFruits3.DataBind();
        ddlFruits3.Items.Insert(0, new ListItem("Select", "0"));
    }
}

private System.Data.DataTable GetFruits()
{
    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.AddRange(new System.Data.DataColumn[] { 
        new System.Data.DataColumn("Id", typeof(int)), 
        new System.Data.DataColumn("Name", typeof(string)) });
    dt.Rows.Add(1, "Mango");
    dt.Rows.Add(2, "Apple");
    dt.Rows.Add(3, "Banana");
    dt.Rows.Add(4, "Orange");

    return dt;
}

protected void SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedFruit = (sender as DropDownList).SelectedItem.Text;
    if (selectedFruit != "Select")
    {
        if ((sender as DropDownList).ID == "ddlFruits1")
        {
            foreach (ListItem item in ddlFruits2.Items)
            {
                if (item.Text == selectedFruit)
                {
                    item.Attributes.Add("disabled", "disabled");
                    break;
                }
            }
            foreach (ListItem item in ddlFruits3.Items)
            {
                if (item.Text == selectedFruit)
                {
                    item.Attributes.Add("disabled", "disabled");
                    break;
                }
            }
        }
        else if ((sender as DropDownList).ID == "ddlFruits2")
        {
            foreach (ListItem item in ddlFruits1.Items)
            {
                if (item.Text == selectedFruit)
                {
                    item.Attributes.Add("disabled", "disabled");
                    break;
                }
            }
            foreach (ListItem item in ddlFruits3.Items)
            {
                if (item.Text == selectedFruit)
                {
                    item.Attributes.Add("disabled", "disabled");
                    break;
                }
            }
        }
        else if ((sender as DropDownList).ID == "ddlFruits3")
        {
            foreach (ListItem item in ddlFruits1.Items)
            {
                if (item.Text == selectedFruit)
                {
                    item.Attributes.Add("disabled", "disabled");
                    break;
                }
            }
            foreach (ListItem item in ddlFruits2.Items)
            {
                if (item.Text == selectedFruit)
                {
                    item.Attributes.Add("disabled", "disabled");
                    break;
                }
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As System.Data.DataTable = GetFruits()
        ddlFruits1.DataSource = dt
        ddlFruits1.DataTextField = "Name"
        ddlFruits1.DataValueField = "Id"
        ddlFruits1.DataBind()
        ddlFruits1.Items.Insert(0, New ListItem("Select", "0"))
        ddlFruits2.DataSource = dt
        ddlFruits2.DataTextField = "Name"
        ddlFruits2.DataValueField = "Id"
        ddlFruits2.DataBind()
        ddlFruits2.Items.Insert(0, New ListItem("Select", "0"))
        ddlFruits3.DataSource = dt
        ddlFruits3.DataTextField = "Name"
        ddlFruits3.DataValueField = "Id"
        ddlFruits3.DataBind()
        ddlFruits3.Items.Insert(0, New ListItem("Select", "0"))
    End If
End Sub

Private Function GetFruits() As System.Data.DataTable
    Dim dt As System.Data.DataTable = New System.Data.DataTable()
    dt.Columns.AddRange(New System.Data.DataColumn() {New System.Data.DataColumn("Id", GetType(Integer)), New System.Data.DataColumn("Name", GetType(String))})
    dt.Rows.Add(1, "Mango")
    dt.Rows.Add(2, "Apple")
    dt.Rows.Add(3, "Banana")
    dt.Rows.Add(4, "Orange")
    Return dt
End Function

Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim selectedFruit As String = (TryCast(sender, DropDownList)).SelectedItem.Text
    If selectedFruit <> "Select" Then
        If (TryCast(sender, DropDownList)).ID = "ddlFruits1" Then
            For Each item As ListItem In ddlFruits2.Items
                If item.Text = selectedFruit Then
                    item.Attributes.Add("disabled", "disabled")
                    Exit For
                End If
            Next
            For Each item As ListItem In ddlFruits3.Items
                If item.Text = selectedFruit Then
                    item.Attributes.Add("disabled", "disabled")
                    Exit For
                End If
            Next
        ElseIf (TryCast(sender, DropDownList)).ID = "ddlFruits2" Then
            For Each item As ListItem In ddlFruits1.Items
                If item.Text = selectedFruit Then
                    item.Attributes.Add("disabled", "disabled")
                    Exit For
                End If
            Next
            For Each item As ListItem In ddlFruits3.Items
                If item.Text = selectedFruit Then
                    item.Attributes.Add("disabled", "disabled")
                    Exit For
                End If
            Next
        ElseIf (TryCast(sender, DropDownList)).ID = "ddlFruits3" Then
            For Each item As ListItem In ddlFruits1.Items
                If item.Text = selectedFruit Then
                    item.Attributes.Add("disabled", "disabled")
                    Exit For
                End If
            Next
            For Each item As ListItem In ddlFruits2.Items
                If item.Text = selectedFruit Then
                    item.Attributes.Add("disabled", "disabled")
                    Exit For
                End If
            Next
        End If
    End If
End Sub

Screenshot