Reputation: 9950
I have two drop down lists with a handful of items.
If the user selects X, X needs to be disabled from the next drop down.
If the user selects Y, Y needs to be disabled from the next drop down.
And vice-versa.
I tried this but it is not working:
protected void ddlSearchColumn1_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList) sender;
string itemSelected = ddl.SelectedValue;
ddlSearchColumn2.Items.FindByValue(itemSelected).Enabled = false;
}
Can anyone give me a hand?
Upvotes: 8
Views: 37464
Reputation: 41
For some reason, this worked for me:
if (ddlState.SelectedValue == "AK")
{
MyDdl.Items.FindByValue("1111111").Enabled = false;
}
Upvotes: 4
Reputation: 9950
I figured I'd add here.
Two things we forgot to add:
EnableViewState="True" AutoPostBack="True"
If you don't add those to the control, the event is not fired.
Upvotes: 0
Reputation:
You were very close:
MarkUP:
List 1: <asp:DropDownList ID="ddlSearchColumn1" runat="server" OnSelectedIndexChanged="ddlSearchColumn1_SelectedIndexChanged" AutoPostBack="true" />
List 2: <asp:DropDownList ID="ddlSearchColumn2" runat="server" AutoPostBack="true" />
Code Behind:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{//JUST PUT SOMETHING IN THE DROPDOWN BOXES
var items1 = new List<ListItem>()
{
new ListItem("Select Option"),
new ListItem("Test 1"),
new ListItem("Test 2"),
new ListItem("Test 3")
};
var items2 = new List<ListItem>()
{
new ListItem("Select Option", ""),
new ListItem("DDL 2 Test 1"),
new ListItem("DDL 2 Test 2"),
new ListItem("DDL 2 Test 3")
};
ddlSearchColumn1.DataSource = items1;
ddlSearchColumn1.DataBind();
ddlSearchColumn2.DataSource = items2;
ddlSearchColumn2.DataBind();
}
}
protected void ddlSearchColumn1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = sender as DropDownList;
if (list == null || list.SelectedValue.ToLower() != "test 1") // OR WHATEVER YOUR CRITERIA IS
return;
ddlSearchColumn2.Items.FindByValue("DDL 2 Test 1").Attributes.Add("Disabled", "Disabled");
}
}
IF you are interested in a client side solution:
With jQuery:
List 1: <asp:DropDownList ID="ddlSearchColumn1" runat="server" />
List 2: <asp:DropDownList ID="ddlSearchColumn2" runat="server" />
<script>
jQuery('#<%= ddlSearchColumn1.ClientID %>').change(function ()
{
if (jQuery(this).val() != 'Test 1')//CHANGE YOUR CRITERIA
return;
jQuery('#<%= ddlSearchColumn2.ClientID %> option[value="DDL 2 Test 1"]').attr('disabled', 'disabled');
});
</script>
Just using javascript no library:
List 1: <asp:DropDownList ID="ddlSearchColumn1" runat="server" />
List 2: <asp:DropDownList ID="ddlSearchColumn2" runat="server" />
<script>
document.getElementById('<%= ddlSearchColumn1.ClientID %>').onchange = function ()
{
var orgDdl = document.getElementById('<%= ddlSearchColumn1.ClientID %>');
var org2ddl = document.getElementById('<%= ddlSearchColumn2.ClientID %>');
if (orgDdl.value != 'Test 1')
return;
for (var i = 0, ii = org2ddl.options.length; i < ii; i++)
{
if (org2ddl.options[i].value == "DDL 2 Test 1")
{
org2ddl.options[i].disabled = "disabled";
break;
}
}
};
</script>
Upvotes: 8
Reputation: 4392
The DropDownList contains a collection of ListItem objects, which have the property Enabled you are trying to set. This property works for ListItems in RadioButtonList or CheckBoxList controls, but NOT DropDownList.
MSDN reference: http://msdn.microsoft.com/en-us/library/vstudio/system.web.ui.webcontrols.listitem.enabled(v=vs.100).aspx
"Note: You cannot use this property to disable a ListItem control in a DropDownList control or ListBox control."
Edit: Answers above solve your problem as you described, and I learned something new :)
Upvotes: 0
Reputation: 2880
protected void ddlFirst_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem item in ddlSecond.Items)
{
if (item.ToString() == ddlFirst.SelectedValue)
{
item.Attributes.Add("disabled", "disabled");
}
}
}
protected void ddlSecond_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem item in ddlFirst.Items)
{
if (item.ToString() == ddlSecond.SelectedValue)
{
item.Attributes.Add("disabled", "disabled");
}
}
}
But I still believe this is client side thing
Upvotes: 0