简体   繁体   English

禁用的下拉列表不起作用(与autopostback和updatepanel相关)C#asp.net

[英]Disabled dropdownlist not working (autopostback & updatepanel related) C# asp.net

After choosing a 2nd list item. 选择第二个列表项之后。 The "Select Category..." for the 3rd drop down list is not disabled any more it became clickable/selectable. 第三个下拉列表的“选择类别...”不再可点击/可选择,不再被禁用。

The "Select Category..." should remain disabled in all drop down list. 在所有下拉列表中,“选择类别...”应保持禁用状态。

I think it has something to do with Autopostback and UpdatePanel. 我认为这与Autopostback和UpdatePanel有关。 because I used updatepanel to keep the modal open when postingback and I used Autopostback to remove the selected item from drop down list. 因为我使用updatepanel在回发时使模式保持打开状态,并且使用了“自动回发”从下拉列表中删除所选项目。

This is my code: 这是我的代码:

<form class="form-horizontal" runat="server">
 <asp:ScriptManager runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server"  UpdateMode="Conditional">
                <ContentTemplate>
           <div class="modal-body">

<div class="form-group">
                            <div class="col-lg-10">
                            <div class="input-group">
                            <div class="input-group-addon"><i class="fa fa-user"></i></div>
                            <asp:DropDownList ID="ddl1" runat="server" class="form-control" MaxLength="50" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" AutoPostBack="true" required >
                            <asp:ListItem Value="" disabled="disabled">Select Category...</asp:ListItem>
                            <asp:ListItem>Artist/Curator</asp:ListItem>
                            <asp:ListItem>MCAD Exhibitor</asp:ListItem>
                            <asp:ListItem>Student</asp:ListItem>
                            <asp:ListItem>Collector/Buyer</asp:ListItem>

                            </asp:DropDownList>
                            </div>
                            </div>
                            </div>


                           <div class="form-group">
                            <div class="col-lg-10">
                            <div class="input-group">
                            <div class="input-group-addon"><i class="fa fa-user"></i></div>
                            <asp:DropDownList ID="ddl2" runat="server" class="form-control" MaxLength="50" OnSelectedIndexChanged="ddl2_SelectedIndexChanged"  AutoPostBack="true" required >
                            <asp:ListItem Value="" disabled="disabled">Select Category...</asp:ListItem>
                            <asp:ListItem>Artist/Curator</asp:ListItem>
                            <asp:ListItem>MCAD Exhibitor</asp:ListItem>
                            <asp:ListItem>Student</asp:ListItem>
                            <asp:ListItem>Collector/Buyer</asp:ListItem>

                            </asp:DropDownList>
                            </div>
                            </div>
                            </div>

                          <div class="form-group">
                            <div class="col-lg-10">
                            <div class="input-group">
                            <div class="input-group-addon"><i class="fa fa-user"></i></div>
                            <asp:DropDownList ID="ddl3" runat="server" class="form-control" MaxLength="50" OnSelectedIndexChanged="ddl3_SelectedIndexChanged" AutoPostBack="true" required >
                            <asp:ListItem Value="" disabled="disabled">Select Category...</asp:ListItem>
                            <asp:ListItem>Artist/Curator</asp:ListItem>
                            <asp:ListItem>MCAD Exhibitor</asp:ListItem>
                            <asp:ListItem>Student</asp:ListItem>
                            <asp:ListItem>Collector/Buyer</asp:ListItem>

                            </asp:DropDownList>
                            </div>
                            </div>
                            </div>
</div>
                    </ContentTemplate>
                    </asp:UpdatePanel>
</form>

My code behind .cs: 我的.cs背后的代码:

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddl2.Items.Remove(ddl1.SelectedItem);
        ddl3.Items.Remove(ddl1.SelectedItem);

        UpdatePanel1.Update();


    }

    protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddl1.Items.Remove(ddl2.SelectedItem);
        ddl3.Items.Remove(ddl2.SelectedItem);

        UpdatePanel1.Update();



    }

    protected void ddl3_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddl1.Items.Remove(ddl3.SelectedItem);
        ddl2.Items.Remove(ddl3.SelectedItem);

        UpdatePanel1.Update();

    }

This is not about your update panel(I tested). 这与您的更新面板(我已测试)无关。 I think this is a bad behavior (or a bug) of drop down list that when you remove its item it select its first item and remove that disability. 我认为这是下拉列表的不良行为(或错误),当您删除其项目时,它会选择其第一项并删除该残障。 To solve this problem you can easily add this jquery to end of your codes: 要解决此问题,您可以轻松地将此jquery添加到代码末尾:

  $('select').each(function ()
  {
       $(this).children().eq(0).attr('disabled', 'disabled');
  });

And if you do not want to use jquery, paste this script after yout dropdowns: 如果不想使用jquery,请在下拉菜单后粘贴以下脚本:

<script>
  var arr = document.getElementsByClassName('form-control');
        for (var i = 0; i < arr.length; i++)
        {
            console.log(arr[i]);
            arr[i].children[0].setAttribute('disabled', 'disabled');
        }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM