Hi Waghmare,
Check this example. Now please take its reference and correct your code.
You need to do the same checking for all the dropdownlist.
HTML
<asp:DropDownList runat="server" ID="ddlFruits1" Width="150px" AutoPostBack="true"
OnSelectedIndexChanged="SelectedIndexChanged1">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlFruits2" Width="150px" AutoPostBack="true"
OnSelectedIndexChanged="SelectedIndexChanged2">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlFruits3" Width="150px">
</asp:DropDownList>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDropDownList(ddlFruits1);
BindDropDownList(ddlFruits2);
BindDropDownList(ddlFruits3);
}
}
private void BindDropDownList(DropDownList ddl)
{
System.Data.DataTable dt = GetFruits();
ddl.Items.Clear();
ddl.DataSource = dt;
ddl.DataTextField = "Name";
ddl.DataValueField = "Id";
ddl.DataBind();
ddl.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 SelectedIndexChanged1(object sender, EventArgs e)
{
string selectedFruit = ddlFruits1.SelectedItem.Text;
if (selectedFruit != "Select")
{
BindDropDownList(ddlFruits2);
for (int i = 0; i < ddlFruits2.Items.Count; i++)
{
if (ddlFruits2.Items[i].Text == selectedFruit)
{
ListItem item = ddlFruits2.Items[i];
ddlFruits2.Items.Remove(item);
i = i - 1;
}
}
BindDropDownList(ddlFruits3);
for (int i = 0; i < ddlFruits3.Items.Count; i++)
{
if (ddlFruits3.Items[i].Text == selectedFruit)
{
ListItem item = ddlFruits3.Items[i];
ddlFruits3.Items.Remove(item);
i = i - 1;
}
}
}
}
protected void SelectedIndexChanged2(object sender, EventArgs e)
{
string selectedFruit1 = ddlFruits1.SelectedItem.Text;
string selectedFruit2 = ddlFruits2.SelectedItem.Text;
if (selectedFruit2 != "Select")
{
BindDropDownList(ddlFruits3);
for (int i = 0; i < ddlFruits3.Items.Count; i++)
{
if (ddlFruits3.Items[i].Text == selectedFruit1 || ddlFruits3.Items[i].Text == selectedFruit2)
{
ListItem item = ddlFruits3.Items[i];
ddlFruits3.Items.Remove(item);
i = i - 1;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDropDownList(ddlFruits1)
BindDropDownList(ddlFruits2)
BindDropDownList(ddlFruits3)
End If
End Sub
Private Sub BindDropDownList(ByVal ddl As DropDownList)
Dim dt As System.Data.DataTable = GetFruits()
ddl.Items.Clear()
ddl.DataSource = dt
ddl.DataTextField = "Name"
ddl.DataValueField = "Id"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("Select", "0"))
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 SelectedIndexChanged1(ByVal sender As Object, ByVal e As EventArgs)
Dim selectedFruit As String = ddlFruits1.SelectedItem.Text
If selectedFruit <> "Select" Then
BindDropDownList(ddlFruits2)
For i As Integer = 0 To ddlFruits2.Items.Count - 1
If ddlFruits2.Items(i).Text = selectedFruit Then
Dim item As ListItem = ddlFruits2.Items(i)
ddlFruits2.Items.Remove(item)
i = i - 1
End If
Next
BindDropDownList(ddlFruits3)
For i As Integer = 0 To ddlFruits3.Items.Count - 1
If ddlFruits3.Items(i).Text = selectedFruit Then
Dim item As ListItem = ddlFruits3.Items(i)
ddlFruits3.Items.Remove(item)
i = i - 1
End If
Next
End If
End Sub
Protected Sub SelectedIndexChanged2(ByVal sender As Object, ByVal e As EventArgs)
Dim selectedFruit1 As String = ddlFruits1.SelectedItem.Text
Dim selectedFruit2 As String = ddlFruits2.SelectedItem.Text
If selectedFruit2 <> "Select" Then
BindDropDownList(ddlFruits3)
For i As Integer = 0 To ddlFruits3.Items.Count - 1
If ddlFruits3.Items(i).Text = selectedFruit1 OrElse ddlFruits3.Items(i).Text = selectedFruit2 Then
Dim item As ListItem = ddlFruits3.Items(i)
ddlFruits3.Items.Remove(item)
i = i - 1
End If
Next
End If
End Sub
Screenshot