在ASP.NET / C#中,我可以编写代码来自动启用DropDownList吗?

dzjeubhm  于 3个月前  发布在  .NET
关注(0)|答案(1)|浏览(77)

我希望下拉列表自动启用一旦当前一个被选中,我写了代码来实现这一点,但没有在页面上的响应。
该代码用于用户从下拉列表中选择多个课程
HTML标记:

<asp:DropDownList ID="DropDownList4" class="form-control form-control-lg"  runat="server" AutoPostBack="True" OnSelectedIndexChanged="List_Change"></asp:DropDownList>
<asp:DropDownList ID="DropDownList5" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList6" class="form-control form-control-lg" Visible="false" AutoPostBack="True" Enabled="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList7" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList8" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList9" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList10" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList11" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList12" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList13" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList14" class="form-control form-control-lg" Enabled="false" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="List_Change" runat="server"></asp:DropDownList>

字符串
C#代码:

static int i = 4;

protected void List_Change(object sender, EventArgs e)
{
    DropDownList ddlid form1.FindControl("DropDownList" + i) as DropDownList;

    while (i <= 14)
    {
        if (ddlid.Enabled && ddlid.Text != "--Select Course--")
        {
           ++i;
           ddlid.Enabled = true;
           ddlid.Visible = true;
        }
        else
        {
           break;
        }
   }
}


页面加载正常,没有任何错误,但一旦我在DropDownList4上选择了课程,就没有响应

q35jwt9p

q35jwt9p1#

逻辑不正确;不需要使用静态变量,循环也是不必要的,最后主要原因是下一个if块总是检查当前下拉列表。
只需检查下一个下拉列表,而不是所有下拉列表:

protected void List_Change(object sender, EventArgs e)
{
    DropDownList current_ddl = sender as DropDownList;
    string id = current_ddl.ID;
    if (current_ddl.SelectedIndex == 0) // "--Select Course--"
    {
        int N = int.Parse(id.Replace("DropDownList", string.Empty));
        do
        {
            string next_id = $"DropDownList{N + 1}";
            DropDownList next_ddl = form1.FindControl(next_id) as DropDownList;
            if ((next_ddl == null))
            {
                break;
            }
            else
            {
                next_ddl.Enabled = false;
                next_ddl.Visible = false;
                next_ddl.SelectedIndex = 0;
                id = next_id;
                N++;
            }

        } while (true);
    }
    else
    {
        int N = int.Parse(id.Replace("DropDownList", string. Empty));
        string next_id = $"DropDownList{N + 1}";
        DropDownList next_ddl = form1.FindControl(next_id) as DropDownList;
        if ((next_ddl != null))
        {
            next_ddl.Enabled = true;
            next_ddl.Visible = true;
        }
    }
}

字符串

相关问题