【问题标题】:Cascading DropDownList in Asp.netAsp.net 中的级联下拉列表
【发布时间】:2017-01-24 03:12:30
【问题描述】:

让我先说这是我努力学习更多 C#/.net 和 Web 开发的努力。所以这可能是显而易见的,但我没有看到它。

我正在尝试创建一个级联 DropDownList。我看过几个例子,但它们似乎都在查询数据库以获取它们的条目。我的要简单得多。根据第一个下拉列表的值,我将手动填充第二个。但是,我似乎没有让 OnSelectedIndexChange 触发。至少,DropDownList1_SelectedIndexChanged 似乎没有被执行。

这是 HTML:

<asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" style="z-index: 1; margin-top: 40px; width: 150px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
        </asp:DropDownList><br/>
        <asp:DropDownList ID="DropDownList2" runat="server" style="z-index: 1; margin-top: 10px; width: 150px">
        </asp:DropDownList><br/>
    </ContentTemplate>
</asp:UpdatePanel>

这里是 C#:

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox2.Text = "Changed";
        DropDownList1.Items.Clear();
        switch (DropDownList1.Text)
        {
            case "Caesar Tools":
                DropDownList2.Items.Add("Caesar Bruteforce");
                DropDownList2.Items.Add("ROT-1");
                DropDownList2.Items.Add("ROT-2");
                DropDownList2.SelectedIndex = 1;
                break;
            case "ASCII Tools":
                DropDownList2.Items.Add("Decimal to ASCII");
                DropDownList2.Items.Add("Hex to ASCII");
                DropDownList2.Items.Add("Binary to ASCII");
                DropDownList2.SelectedIndex = 0;
                break;
            default:
                break;
        }
    }

我错过了什么?

谢谢!

【问题讨论】:

标签: c# asp.net


【解决方案1】:

嗯,您的代码中实际上存在错误/错字。如果你仔细看DropDownList1_SelectedIndexChanged这个方法,你会发现这条线很可疑。

DropDownList1.Items.Clear();

我的猜测是,当 DropDownList1 控件的选择发生更改时,您想清除 DropDownList2 控件的项目,而不是 DropDownList1 控件。

我附上了下面的标记和代码,供您参考。

ASPX 标记

<asp:ScriptManager runat="server" ID="sm1"></asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" Style="z-index: 1; margin-top: 40px; width: 150px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
                        <asp:ListItem Text="" Value=""></asp:ListItem>
                        <asp:ListItem Text="Caesar Tools" Value="Caesar Tools"></asp:ListItem>
                        <asp:ListItem Text="ASCII Tools" Value="ASCII Tools"></asp:ListItem>
                    </asp:DropDownList><br />
                    <asp:DropDownList ID="DropDownList2" runat="server" Style="z-index: 1; margin-top: 10px; width: 150px">
                    </asp:DropDownList><br />
                </ContentTemplate>
            </asp:UpdatePanel>

ASPX.cs 代码背后

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList2.Items.Clear();
            switch (DropDownList1.Text)
            {
                case "Caesar Tools":
                    DropDownList2.Items.Add("Caesar Bruteforce");
                    DropDownList2.Items.Add("ROT-1");
                    DropDownList2.Items.Add("ROT-2");
                    DropDownList2.SelectedIndex = 1;
                    DropDownList2.DataBind();
                    break;
                case "ASCII Tools":
                    DropDownList2.Items.Add("Decimal to ASCII");
                    DropDownList2.Items.Add("Hex to ASCII");
                    DropDownList2.Items.Add("Binary to ASCII");
                    DropDownList2.SelectedIndex = 0;
                    DropDownList2.DataBind();
                    break;
                default:
                    break;
            }
        }

希望这会有所帮助。

【讨论】:

  • 这就是当你被锁定在一个可能的问题上而没有全盘考虑时会发生的情况。这样就彻底解决了。谢谢!
猜你喜欢
  • 2023-03-30
  • 2014-01-12
  • 2014-10-23
  • 2014-05-22
  • 2016-04-13
  • 2021-03-04
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
相关资源
最近更新 更多