试试下面的代码。
<asp:DropDownList runat="server" AutoPostBack="true" ID="ddlCategories" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged">
<asp:ListItem Text="All" Value="All" />
<asp:ListItem Text="Nokia" Value="Nokia" />
<asp:ListItem Text="Samsung" Value="Samsung" />
</asp:DropDownList>
<asp:DropDownList runat="server" AppendDataBoundItems="true" ID="ddlSubCategories">
<asp:ListItem Text="All" Value="All" />
</asp:DropDownList>
因为您没有使用数据库,所以我们必须对值进行硬编码。
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
ddlSubCategories.Items.Clear();
if (string.Compare(ddlCategories.SelectedValue, "Nokia", true) == 0)
{
ddlSubCategories.Items.Add(new ListItem("3310", "3310"));
ddlSubCategories.Items.Add(new ListItem("N95", "N95"));
}
else if (string.Compare(ddlCategories.SelectedValue, "Samsung", true) == 0)
{
ddlSubCategories.Items.Add(new ListItem("Galaxy Ace", "Galaxy Ace"));
ddlSubCategories.Items.Add(new ListItem("Galaxy SII", "Galaxy SII"));
}
ddlSubCategories.Items.Insert(0, new ListItem("All", "All"));
}
Hope it helps. If you want to go for javascript route find below link.
Populate one dropdown based on selection in another