我只想要一个没有选定项目的 ASP.NET DropDownList。到目前为止,将 SelectedIndex 设置为 -1 没有任何作用。我正在使用 Framework 3.5 和 AJAX,即此 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)...我做错了什么?蚂蚁的帮助将不胜感激!

有帮助吗?

解决方案

请记住,如果在执行 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", ""));

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

正如所提到的,在下拉菜单中总是会选择一个项目(我相信列表框是不同的),如果没有明确选择,则默认为最上面的一个。

其他提示

是可以设置的 选定索引 的财产 下拉列表 至 -1(即e.清除选择)使用客户端脚本:

<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>

我正在阅读以下内容: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 仅适用于获取而不适用于设置索引?如果是这样,我将使用你的“补丁”。

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

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

作为我的第一个列表项,并相应地进行。

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

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

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

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

  1. 创建 DropDown 列表并指定初始 ListItem
  2. AppendDataBoundItemstrue 以便附加新项目。
<asp:DropDownList ID="YourID" DataSourceID="DSID" AppendDataBoundItems="true"> 
<asp:ListItem Text="All" Value="%"></asp:ListItem> 
</asp:DropDownList>

请尝试以下语法:

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top