<p>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:</p>

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

<p>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!</p>

[{'answer_id': 81246, 'author': 'bryansh', 'author_id': 211367, 'author_profile': 'https://Stackoverflow.com/users/211367', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m pretty sure that dropdown has to have some item selected; I usually add an empty list item</p>\n\n<p>this.myDropDownList.Items.Add("");</p>\n\n<p>As my first list item, and proceed accordingly.</p>\n'}, {'answer_id': 81297, 'author': 'Aaron Powell', 'author_id': 11388, 'author_profile': 'https://Stackoverflow.com/users/11388', 'pm_score': 2, 'selected': False, 'text': '<p>The selectedIndex can only be -1 when the control is first initalised and there is no items within the collection.</p>\n\n<p>It\'s not possible to have no item selected in a web drop down list as you would on a WinForm.</p>\n\n<p>I find it\'s best to have:\nthis.myDropDownList.Items.Add(new ListItem("Please select...", ""));</p>\n\n<p>This way I convey to the user that they need to select an item, and you can check SelectedIndex == 0 to validate</p>\n'}, {'answer_id': 81351, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I am reading the following:\n<a href="http://msdn.microsoft.com/en-us/library/a5kfekd2.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/a5kfekd2.aspx</a></p>\n\n<p>It says:\nTo 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. </p>\n\n<p>In the same time, at <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.selectedindex(VS.80).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.selectedindex(VS.80).aspx</a> we see:</p>\n\n<p>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.</p>\n\n<p>Perhaps -1 is valid just for getting and not for setting the index? If so, I will use your \'patch\'.</p>\n'}, {'answer_id': 81379, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': True, 'text': '<p>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...</p>\n\n<pre><code>myDropDownList.DataSource = DataAccess.GetDropDownItems(); // Psuedo Code\nmyDropDownList.DataTextField = "Value";\nmyDropDownList.DataValueField = "Id";\nmyDropDownList.DataBind();\n\nmyDropDownList.Items.Insert(0, new ListItem("Please select", ""));\n</code></pre>\n\n<p>Will add the \'Please select\' drop down item to the top.</p>\n\n<p>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.</p>\n'}, {'answer_id': 201012, 'author': 'Alexander Prokofyev', 'author_id': 11256, 'author_profile': 'https://Stackoverflow.com/users/11256', 'pm_score': 3, 'selected': False, 'text': '<p>It\'s possible to set <em>selectedIndex</em> property of <em>DropDownList</em> to -1 (i. e. clear selection) using client-side script:</p>\n\n<pre><code>&lt;form id="form1" runat="server"&gt;\n    &lt;asp:DropDownList ID="DropDownList1" runat="server"&gt;\n        &lt;asp:ListItem Value="A"&gt;&lt;/asp:ListItem&gt;\n        &lt;asp:ListItem Value="B"&gt;&lt;/asp:ListItem&gt;\n        &lt;asp:ListItem Value="C"&gt;&lt;/asp:ListItem&gt;\n    &lt;/asp:DropDownList&gt;\n    &lt;button id="СlearButton"&gt;Clear&lt;/button&gt;\n&lt;/form&gt;\n\n&lt;script src="jquery-1.2.6.js" type="text/javascript"&gt;&lt;/script&gt;\n&lt;script type="text/javascript"&gt;\n    $(document).ready(function()\n    {\n        $("#СlearButton").click(function()\n        {\n            $("#DropDownList1").attr("selectedIndex", -1); // pay attention to property casing\n        })\n\n        $("#ClearButton").click();\n    })\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 11666108, 'author': 'Vimal Patel', 'author_id': 1488039, 'author_profile': 'https://Stackoverflow.com/users/1488039', 'pm_score': 0, 'selected': False, 'text': '<p>Please try below syntax:</p>\n\n<pre><code>DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Select"))\n</code></pre>\n\n<p>or</p>\n\n<pre><code>DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("SelectText"))\n</code></pre>\n\n<p>or </p>\n\n<pre><code>DropDownList1.Items.FindByText("Select").selected =true\n</code></pre>\n\n<p>For more info :\n<a href="http://vimalpatelsai.blogspot.in/2012/07/dropdownlistselectedindex-1-problem.html" rel="nofollow">http://vimalpatelsai.blogspot.in/2012/07/dropdownlistselectedindex-1-problem.html</a></p>\n'}, {'answer_id': 13221668, 'author': 'Lemaire Stewart', 'author_id': 1798517, 'author_profile': 'https://Stackoverflow.com/users/1798517', 'pm_score': 2, 'selected': False, 'text': '<ol>\n<li>Create your DropDown list and specify an initial ListItem</li>\n<li>Set <code>AppendDataBoundItems</code> to <code>true</code> so that new items get appended.<br></li>\n</ol>\n\n<blockquote>\n<pre><code>&lt;asp:DropDownList ID="YourID" DataSourceID="DSID" AppendDataBoundItems="true"&gt; \n&lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt; \n&lt;/asp:DropDownList&gt;\n</code></pre>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/81214', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']