views:

11817

answers:

6

Hello, I just want an ASP.NET DropDownList with no selected item. Setting SelectedIndex to -1 is of no avail, so far. I am using Framework 3.5 with AJAX, i.e. this DropDownList is within an UpdatePanel. Here is what I am doing:

 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");
 }

The moment I add an element in the DropDown, its SelectedIndex changes to 0 and can be no more set to -1 (I tried calling SelectedIndex after adding items as well)... What I am doing wrong? Ant help would be appreciated!

+1  A: 

I'm pretty sure that dropdown has to have some item selected; I usually add an empty list item

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

As my first list item, and proceed accordingly.

bryansh
+1  A: 

The selectedIndex can only be -1 when the control is first initalised and there is no items within the collection.

It's not possible to have no item selected in a web drop down list as you would on a WinForm.

I find it's best to have: this.myDropDownList.Items.Add(new ListItem("Please select...", ""));

This way I convey to the user that they need to select an item, and you can check SelectedIndex == 0 to validate

Slace
A: 

Hi bryansh

I am reading the following: http://msdn.microsoft.com/en-us/library/a5kfekd2.aspx

It says: To get the index value of the selected item, read the value of the SelectedIndex property. The index is zero-based. If nothing has been selected, the value of the property is -1.

In the same time, at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.selectedindex(VS.80).aspx we see:

Use the SelectedIndex property to programmatically specify or determine the index of the selected item from the DropDownList control. An item is always selected in the DropDownList control. You cannot clear the selection from every item in the list at the same time.

Perhaps -1 is valid just for getting and not for setting the index? If so, I will use your 'patch'.

+3  A: 

Bare in mind myDropDownList.Items.Add will add a new Listitem element at the bottom if you call it after performing a DataSource/DataBind call so use myDropDownList.Items.Insert method instead eg...

myDropDownList.DataSource = DataAccess.GetDropDownItems(); // Psuedo Code
myDropDownList.DataTextField = "Value";
myDropDownList.DataValueField = "Id";
myDropDownList.DataBind();

myDropDownList.Items.Insert(0, new ListItem("Please select", ""));

Will add the 'Please select' drop down item to the top.

And as mentioned there will always be exactly one Item selected in a drop down (ListBoxes are different I believe), and this defaults to the top one if none are explicitly selected.

A: 

10x to both of you!

+2  A: 

It's possible to set selectedIndex property of DropDownList to -1 (i. e. clear selection) using client-side script:

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