你非常接近:
标记:
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 没有库:
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>