ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Disable selected DropDownList Item (Option) in others using C# and VB.Net in ASP.Net<p><a class="username" rel="nid@patel">Hi </a>nid@patel,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>Here i have binded the DropDown&nbsp;using DataTable. You need to bind it from Database.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlFruits1&#34; AutoPostBack=&#34;true&#34; Width=&#34;150px&#34; OnSelectedIndexChanged=&#34;SelectedIndexChanged&#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlFruits2&#34; AutoPostBack=&#34;true&#34; Width=&#34;150px&#34; OnSelectedIndexChanged=&#34;SelectedIndexChanged&#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlFruits3&#34; AutoPostBack=&#34;true&#34; Width=&#34;150px&#34; OnSelectedIndexChanged=&#34;SelectedIndexChanged&#34;&gt; &lt;/asp:DropDownList&gt;</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { System.Data.DataTable dt = GetFruits(); ddlFruits1.DataSource = dt; ddlFruits1.DataTextField = &#34;Name&#34;; ddlFruits1.DataValueField = &#34;Id&#34;; ddlFruits1.DataBind(); ddlFruits1.Items.Insert(0, new ListItem(&#34;Select&#34;, &#34;0&#34;)); ddlFruits2.DataSource = dt; ddlFruits2.DataTextField = &#34;Name&#34;; ddlFruits2.DataValueField = &#34;Id&#34;; ddlFruits2.DataBind(); ddlFruits2.Items.Insert(0, new ListItem(&#34;Select&#34;, &#34;0&#34;)); ddlFruits3.DataSource = dt; ddlFruits3.DataTextField = &#34;Name&#34;; ddlFruits3.DataValueField = &#34;Id&#34;; ddlFruits3.DataBind(); ddlFruits3.Items.Insert(0, new ListItem(&#34;Select&#34;, &#34;0&#34;)); } } private System.Data.DataTable GetFruits() { System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.AddRange(new System.Data.DataColumn[] { new System.Data.DataColumn(&#34;Id&#34;, typeof(int)), new System.Data.DataColumn(&#34;Name&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;Mango&#34;); dt.Rows.Add(2, &#34;Apple&#34;); dt.Rows.Add(3, &#34;Banana&#34;); dt.Rows.Add(4, &#34;Orange&#34;); return dt; } protected void SelectedIndexChanged(object sender, EventArgs e) { string selectedFruit = (sender as DropDownList).SelectedItem.Text; if (selectedFruit != &#34;Select&#34;) { if ((sender as DropDownList).ID == &#34;ddlFruits1&#34;) { foreach (ListItem item in ddlFruits2.Items) { if (item.Text == selectedFruit) { item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;); break; } } foreach (ListItem item in ddlFruits3.Items) { if (item.Text == selectedFruit) { item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;); break; } } } else if ((sender as DropDownList).ID == &#34;ddlFruits2&#34;) { foreach (ListItem item in ddlFruits1.Items) { if (item.Text == selectedFruit) { item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;); break; } } foreach (ListItem item in ddlFruits3.Items) { if (item.Text == selectedFruit) { item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;); break; } } } else if ((sender as DropDownList).ID == &#34;ddlFruits3&#34;) { foreach (ListItem item in ddlFruits1.Items) { if (item.Text == selectedFruit) { item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;); break; } } foreach (ListItem item in ddlFruits2.Items) { if (item.Text == selectedFruit) { item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;); break; } } } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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 = &#34;Name&#34; ddlFruits1.DataValueField = &#34;Id&#34; ddlFruits1.DataBind() ddlFruits1.Items.Insert(0, New ListItem(&#34;Select&#34;, &#34;0&#34;)) ddlFruits2.DataSource = dt ddlFruits2.DataTextField = &#34;Name&#34; ddlFruits2.DataValueField = &#34;Id&#34; ddlFruits2.DataBind() ddlFruits2.Items.Insert(0, New ListItem(&#34;Select&#34;, &#34;0&#34;)) ddlFruits3.DataSource = dt ddlFruits3.DataTextField = &#34;Name&#34; ddlFruits3.DataValueField = &#34;Id&#34; ddlFruits3.DataBind() ddlFruits3.Items.Insert(0, New ListItem(&#34;Select&#34;, &#34;0&#34;)) 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(&#34;Id&#34;, GetType(Integer)), New System.Data.DataColumn(&#34;Name&#34;, GetType(String))}) dt.Rows.Add(1, &#34;Mango&#34;) dt.Rows.Add(2, &#34;Apple&#34;) dt.Rows.Add(3, &#34;Banana&#34;) dt.Rows.Add(4, &#34;Orange&#34;) 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 &lt;&gt; &#34;Select&#34; Then If (TryCast(sender, DropDownList)).ID = &#34;ddlFruits1&#34; Then For Each item As ListItem In ddlFruits2.Items If item.Text = selectedFruit Then item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;) Exit For End If Next For Each item As ListItem In ddlFruits3.Items If item.Text = selectedFruit Then item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;) Exit For End If Next ElseIf (TryCast(sender, DropDownList)).ID = &#34;ddlFruits2&#34; Then For Each item As ListItem In ddlFruits1.Items If item.Text = selectedFruit Then item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;) Exit For End If Next For Each item As ListItem In ddlFruits3.Items If item.Text = selectedFruit Then item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;) Exit For End If Next ElseIf (TryCast(sender, DropDownList)).ID = &#34;ddlFruits3&#34; Then For Each item As ListItem In ddlFruits1.Items If item.Text = selectedFruit Then item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;) Exit For End If Next For Each item As ListItem In ddlFruits2.Items If item.Text = selectedFruit Then item.Attributes.Add(&#34;disabled&#34;, &#34;disabled&#34;) Exit For End If Next End If End If End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/vLEOSRm.gif" alt="" width="472" height="117" /></span></strong></p>https://www.aspforums.net:443/Threads/143074/Disable-selected-DropDownList-Item-Option-in-others-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/143074/Disable-selected-DropDownList-Item-Option-in-others-using-C-and-VBNet-in-ASPNet/Mon, 05 Aug 2019 07:28:05 GMT