“DropDownList.SelectedIndex = -1”问题

时间:2008-09-17 08:46:22

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

我只想要一个没有选定项目的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)...我做错了什么?蚂蚁帮助将不胜感激!

7 个答案:

答案 0 :(得分:18)

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

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

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

答案 1 :(得分:5)

可以使用客户端脚本将 DropDownList selectedIndex 属性设置为-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>

答案 2 :(得分:4)

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

答案 3 :(得分:2)

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

this.myDropDownList.Items.Add( “”);

作为我的第一个清单项目,并据此进行。

答案 4 :(得分:2)

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

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

我觉得最好有: this.myDropDownList.Items.Add(new ListItem(“Please select ...”,“”));

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

答案 5 :(得分:2)

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

答案 6 :(得分:0)

请尝试以下语法:

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