我有两个带有少量项目的下拉列表。
如果用户选择X,则需要从下一个下拉列表中禁用X.
如果用户选择Y,则需要从下一个下拉列表中禁用Y.
反之亦然。
我尝试了这个但是没有用:
protected void ddlSearchColumn1_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList) sender;
string itemSelected = ddl.SelectedValue;
ddlSearchColumn2.Items.FindByValue(itemSelected).Enabled = false;
}
任何人都可以帮我一把吗?
答案 0 :(得分:8)
你非常接近:
标记:
List 1: <asp:DropDownList ID="ddlSearchColumn1" runat="server" OnSelectedIndexChanged="ddlSearchColumn1_SelectedIndexChanged" AutoPostBack="true" />
List 2: <asp:DropDownList ID="ddlSearchColumn2" runat="server" AutoPostBack="true" />
代码背后:
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");
}
}
如果您对客户端解决方案感兴趣:
使用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>
只使用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>
答案 1 :(得分:4)
出于某种原因,这对我有用:
if (ddlState.SelectedValue == "AK")
{
MyDdl.Items.FindByValue("1111111").Enabled = false;
}
答案 2 :(得分:0)
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");
}
}
}
但我仍然相信这是客户端的事情
答案 3 :(得分:0)
DropDownList包含一组ListItem对象,它们具有您尝试设置的属性Enabled。此属性适用于RadioButtonList或CheckBoxList控件中的ListItem,但不适用于DropDownList。
“注意:您不能使用此属性来禁用ListItem控件 DropDownList控件或ListBox控件。“
编辑:上面的答案解决了你所描述的问题,我学到了新东西:)
答案 4 :(得分:-1)
我想我会在这里添加。
我们忘记添加两件事:
EnableViewState =“True”AutoPostBack =“True”
如果您不将这些添加到控件中,则不会触发该事件。