【问题标题】:How to select DropDown List value to obtain results in another drop down list in Grid View如何选择下拉列表值以在网格视图中的另一个下拉列表中获取结果
【发布时间】:2014-06-17 16:33:55
【问题描述】:
我的总体目标是能够有一个下拉列表,当在下拉列表中选择一个值时,我将能够在下拉列表 2 中为该选项选择特定值,依此类推。最终在网格视图中显示独特的结果。
例如,
DropDown List 1
Cars
Food
Colors <- Selected Value
DropDown List 2
Red
Blue <- Selected Value
Black
Grid View Results
Specific Colors Number Available
Baby Blue 2
Night Blue 5
Sky Blue 0
Dark Blue 3
【问题讨论】:
标签:
c#
asp.net
gridview
drop-down-menu
【解决方案1】:
如果我理解您想要正确执行的操作,您可以将 DropDownList1 中的初始值设置为无,然后创建一个事件来处理 DropDownList1 的 SelectedIndexChanged 事件,该事件可以根据所选索引进行切换。例如:
<asp:DropDownList runat="server" ID="DropDownList1" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_OnSelectedIndexChanged"> put all your list items </asp>
然后在您的代码隐藏文件中:
protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e)
{
List<string> elements; // a List containing the elements you want in the second drop own menu (you will need one for each possible set of elements)
switch(DropDownList1.SelectedValue)
{
case "Colors":
DropDownList2.Items.Clear();
DropdownList2.Items.Add(elements);
break;
// And then your other cases here
}
}
然后在 DropDownList2 上选择索引时执行类似的函数调用来设置您的网格视图。