【问题标题】:"DropDownList.SelectedIndex = -1" problem“DropDownList.SelectedIndex = -1”问题
【发布时间】:2023-03-10 01:00:01
【问题描述】:

我只想要一个没有选定项目的 ASP.NET DropDownList。到目前为止,将 SelectedIndex 设置为 -1 无济于事。我正在使用带有 AJAX 的 Framework 3.5,即这个 DropDownList 在 UpdatePanel 中。 这是我正在做的事情:

    protected void Page_Load (object sender, EventArgs e)
    {
        this.myDropDownList.SelectedIndex = -1;
        this.myDropDownList.ClearSelection();

        this.myDropDownList.Items.Add("Item1");
        this.myDropDownList.Items.Add("Item2");
    }

当我在 DropDown 中添加一个元素时,它的 SelectedIndex 更改为 0,并且不能再设置为 -1(我也尝试在添加项目后调用 SelectedIndex)...我做错了什么?蚂蚁帮助将不胜感激!

【问题讨论】:

    标签: asp.net webforms drop-down-menu


    【解决方案1】:
    1. 创建您的下拉列表并指定初始 ListItem
    2. AppendDataBoundItems 设置为true 以便追加新项目。
    <asp:DropDownList ID="YourID" DataSourceID="DSID" AppendDataBoundItems="true"> 
    <asp:ListItem Text="All" Value="%"></asp:ListItem> 
    </asp:DropDownList>
    

    【讨论】:

      【解决方案2】:

      请尝试以下语法:

      DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Select"))
      

      DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("SelectText"))
      

      DropDownList1.Items.FindByText("Select").selected =true
      

      更多信息: http://vimalpatelsai.blogspot.in/2012/07/dropdownlistselectedindex-1-problem.html

      【讨论】:

        【解决方案3】:

        可以使用客户端脚本将 DropDownListselectedIndex 属性设置为 -1(即清除选择):

        <form id="form1" runat="server">
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Value="A"></asp:ListItem>
                <asp:ListItem Value="B"></asp:ListItem>
                <asp:ListItem Value="C"></asp:ListItem>
            </asp:DropDownList>
            <button id="СlearButton">Clear</button>
        </form>
        
        <script src="jquery-1.2.6.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function()
            {
                $("#СlearButton").click(function()
                {
                    $("#DropDownList1").attr("selectedIndex", -1); // pay attention to property casing
                })
        
                $("#ClearButton").click();
            })
        </script>
        

        【讨论】:

        • 小评论:对于 jQuery 1.6 或更新版本, $(elem).attr("selectedIndex") 将不再工作,因为 selectedIndex 是一个属性,而不是一个属性。改用 $(emel).prop("selectedIndex", -1)。
        【解决方案4】:

        请记住,如果您在执行 DataSource/DataBind 调用后调用 myDropDownList.Items.Add 将在底部添加一个新的 Listitem 元素,因此请改用 myDropDownList.Items.Insert 方法,例如...

        myDropDownList.DataSource = DataAccess.GetDropDownItems(); // Psuedo Code
        myDropDownList.DataTextField = "Value";
        myDropDownList.DataValueField = "Id";
        myDropDownList.DataBind();
        
        myDropDownList.Items.Insert(0, new ListItem("Please select", ""));
        

        将“请选择”下拉项添加到顶部。

        如前所述,在下拉列表中总是会选择一个项(我相信 ListBox 不同),如果没有明确选择,则默认为最前面的一项。

        【讨论】:

          【解决方案5】:

          我正在阅读以下内容: http://msdn.microsoft.com/en-us/library/a5kfekd2.aspx

          它说: 要获取所选项目的索引值,请读取 SelectedIndex 属性的值。该索引从零开始。如果未选择任何内容,则该属性的值为 -1。

          同时,在http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.selectedindex(VS.80).aspx,我们看到:

          使用 SelectedIndex 属性以编程方式指定或确定 DropDownList 控件中选定项的索引。在 DropDownList 控件中始终选择一个项目。您不能同时清除列表中每个项目的选择。

          也许 -1 仅用于获取而不用于设置索引?如果是这样,我将使用您的“补丁”。

          【讨论】:

            【解决方案6】:

            当控件第一次初始化并且集合中没有项目时,selectedIndex 只能为 -1。

            不可能像在 WinForm 上那样在 Web 下拉列表中不选择任何项目。

            我发现最好有: this.myDropDownList.Items.Add(new ListItem("请选择...", ""));

            这样我向用户传达他们需要选择一个项目,您可以检查 SelectedIndex == 0 来验证

            【讨论】:

              【解决方案7】:

              我很确定下拉菜单必须选择一些项目;我通常会添加一个空列表项

              this.myDropDownList.Items.Add("");

              作为我的第一个列表项,然后进行相应的操作。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2010-11-15
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-03-23
                • 2012-02-09
                相关资源
                最近更新 更多