根据键填充/绑定DropDownList值

时间:2018-06-11 12:05:51

标签: c# asp.net

我尝试根据另一个DropDownList值更改选择/显示DropDownList中的单个值组。

我有一个同步类型和操作类型DDL ,我想绑定/只显示操作类型DDL中的键值,这些键值在First DDL中选择,即同步类型DDL (注 - 同步类型DDL的值类似于操作类型DDL的键)

以下是同步类型HTML标记:

<div>
    <label>
        Sync Type
    </label>
    <asp:DropDownList runat="server" ID="dllSyncType" AutoPostBack="true" OnSelectedIndexChanged="ddlChannel_SelectedIndexChanged">
        <asp:ListItem Text="Property" Value="1"></asp:ListItem>
        <asp:ListItem Text="Room" Value="2"></asp:ListItem>
        <asp:ListItem Text="Rate" Value="3"></asp:ListItem>
        <asp:ListItem Text="Product" Value="4"></asp:ListItem>
        <asp:ListItem Text="Open/Close Property" Value="5"></asp:ListItem>
        <asp:ListItem Text="Sync All" Value="6"></asp:ListItem>
    </asp:DropDownList>
</div>

和操作类型DDL:

<div>
    <label>
        <%=Resources.Common.OperationType%>
    </label>
    <asp:DropDownList runat="server" ID="ddlOperationType">
    </asp:DropDownList>
</div>

我正在使用包含硬编码键值记录集的Dictionary来绑定操作类型DDL中的值,如下所示。

Dictionary<string, string> dicOperationTypes = new Dictionary<string, string>();
dicOperationTypes.Add("-1", "Select");
dicOperationTypes.Add("Property", "Check");
dicOperationTypes.Add("Property", "Open");
dicOperationTypes.Add("Property", "Close");

dicOperationTypes.Add("Room", "Activate");
dicOperationTypes.Add("Room", "Deactivate");

dicOperationTypes.Add("Rate", "Update");
dicOperationTypes.Add("Rate", "Activate");
dicOperationTypes.Add("Rate", "Deactivate");
ddlOperationType.DataSource = dicOperationTypes;
ddlOperationType.DataValueField = "Key";
ddlOperationType.DataTextField = "Value";
ddlOperationType.DataBind();

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您需要根据所选的dllSyncType在dicOperationTypes中设置相关数据,如以下代码示例所示:

 public void ddlChannel_SelectedIndexChanged(object sender, EventArgs e)
 {
        Dictionary<string, string> dicOperationTypes = new Dictionary<string, string>();
        dicOperationTypes.Add("Select", "0");
        switch (dllSyncType.SelectedValue)
        {
            case "1":
                dicOperationTypes.Add("Check", "1");
                dicOperationTypes.Add("Open", "2");
                dicOperationTypes.Add("Close", "3");
                break;
            case "2":
                dicOperationTypes.Add("Activate", "4");
                dicOperationTypes.Add("Deactivate", "5");
                break;
            case "3":
                dicOperationTypes.Add("Update", "6");
                dicOperationTypes.Add("Activate", "4");
                dicOperationTypes.Add("Deactivate", "5");
                break;
            case "4":
                // related Operation Types with Product
                break;
            case "5":
                // related Operation Types with Open/Close Property
                break;
            case "6":
                // related Operation Types with Sync All
                break;
            default:
                break;
        }
        ddlOperationType.DataSource = dicOperationTypes;
        ddlOperationType.DataValueField = "Value";
        ddlOperationType.DataTextField = "Key";
        ddlOperationType.DataBind();
 }

答案 1 :(得分:0)

您无法为Dictionary<string, string>传递相同的密钥。我认为你得到编译时错误。

像:     System.ArgumentException:'已经添加了具有相同键的项。'