【问题标题】:If statement not executing some part of the code如果语句不执行代码的某些部分
【发布时间】:2019-07-24 12:32:17
【问题描述】:

当在 dropdownlist1 上选择第 1 项时,在下拉列表 2 上应禁用带有“02”和“03”星号的项目,当在下拉列表 1 上选择下拉列表 2 上的第 2 项时,应禁用以“01”开头的项目并且“03”应该被禁用

protected void Page_Load(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.Value == "01")
    {
        DropDownList2.Items.Cast<ListItem>()
        .Where(x => (x.Value.Substring(0, 2) == "02") || (x.Value.Substring(0, 2) == "03"))
        .ToList()
        .ForEach(x => x.Enabled = false);
    }
    else
    {
        DropDownList2.Items.Cast<ListItem>()
        .Where(x => (x.Value.Substring(0, 2) == "02") || (x.Value.Substring(0, 2) == "03"))
        .ToList()
        .ForEach(x => x.Enabled = true);

        if (DropDownList1.SelectedItem.Value == "02")
        {
            DropDownList2.Items.Cast<ListItem>()
            .Where(x => (x.Value.Substring(0, 2) == "01") || (x.Value.Substring(0, 2) == "03"))
            .ToList()
            .ForEach(x => x.Enabled = false);
        }
        else
        {
            DropDownList2.Items.Cast<ListItem>()
            .Where(x => (x.Value.Substring(0, 2) == "01") || (x.Value.Substring(0, 2) == "03"))
            .ToList()
            .ForEach(x => x.Enabled = true);
        }
    }
}

protected void DropDownList1_ItemChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.Value == "01")
    {
        DropDownList2.Items.Cast<ListItem>()
        .Where(x => (x.Value.Substring(0, 2) == "02") || (x.Value.Substring(0, 2) == "03"))
        .ToList()
        .ForEach(x => x.Enabled = false);
    }
    else
    {
        DropDownList2.Items.Cast<ListItem>()
        .Where(x => (x.Value.Substring(0, 2) == "02") || (x.Value.Substring(0, 2) == "03"))
        .ToList()
        .ForEach(x => x.Enabled = true);

        if (DropDownList1.SelectedItem.Value == "02")
        {
            DropDownList2.Items.Cast<ListItem>()
            .Where(x => (x.Value.Substring(0, 2) == "01") || (x.Value.Substring(0, 2) == "03"))
            .ToList()
            .ForEach(x => x.Enabled = false);
        }
        else
        {
            DropDownList2.Items.Cast<ListItem>()
            .Where(x => (x.Value.Substring(0, 2) == "01") || (x.Value.Substring(0, 2) == "03"))
            .ToList()
            .ForEach(x => x.Enabled = true);
        }
    }
}

页面加载按预期工作,下拉列表 2 上的项目正确显示,但是一旦我将下拉列表 1 上的项目更改为项目 2 并再次更改为项目 1,下拉列表 2 根本不显示任何内容,但下拉列表 1 上的项目 2 正在工作一般。

【问题讨论】:

  • 这是学习使用调试器的好时机!设置断点,逐步查看问题所在(即逻辑是否错误?您的代码有问题吗?等等)
  • ItemChanged 事件将由 Load 事件调用。您可能希望在 Load Event 结束时而不是在构造函数中注册 Change Event。

标签: c# asp.net


【解决方案1】:

就像 Greg 说的,你应该学会调试。

这就是你在做什么......

您在第二个 ListBox 中有一个项目列表:

01, 02, 03

第 1 步 - 在 ListBox1 中选择 01:停用 0203

ListBox2.Items = { 01 }

第 2 步 - 在 ListBox1 中选择 03:激活 0203,停用 0103

ListBox2.Items = { 02 }

第 3 步 - 在 ListBox1 中选择 01:停用 0203

ListBox2.Items = { }

结果:一开始你忘了激活一切。

更简单的解决方案

每次为每个元素设置Enabled。这样你就不必在更改之前为状态而烦恼..而且它更短..

protected void DropDownList1_ItemChanged(object sender, EventArgs e)
{
    DropDownList2.Items.Cast<ListItem>()
        .ToList()
        .ForEach(x => x.Enabled = (DropDownList1.SelectedItem.Value == x.Value.Substring(0, 2)));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2012-12-31
    相关资源
    最近更新 更多