Dropdownlist
read its values from database and show them. I want when page loaded dropdownlist
showing nothing (i.e selectedindex
= -1) and user select values. any idea?
Asked
Active
Viewed 377 times
-1

Mahdi_Nine
- 14,205
- 26
- 82
- 117
-
You have the code there what is the problem? On page load `myDDL.SelectedIndex=-1;` add a required field validator to it or write code to ensure the user has selected a value. – JonH Aug 09 '12 at 16:18
-
myDDL.SelectedIndex=-1; has no effect! – Mahdi_Nine Aug 09 '12 at 16:19
-
I believe this post has an appropriate answer http://stackoverflow.com/questions/521334/databound-drop-down-list-initial-value . – Luke Baughan Aug 09 '12 at 16:20
-
Mahdi_Nine it has no effect because it is based on what items you have in your list, which you haven't posted how it gets filled (dataset, datareader, etc). You can fill it with an empty element from the database side or create an additional list item. – JonH Aug 09 '12 at 16:23
-
i know that can add another item.but i don't want that! – Mahdi_Nine Aug 09 '12 at 16:25
-
@Mahdi_Nine You dont want that? So how do you get an empty item in the list??? Do it on the database side if you are dealing with a database. – JonH Aug 09 '12 at 16:26
-
it read information from database but i think maybe way to show empty without add empty item!thanx. – Mahdi_Nine Aug 09 '12 at 16:29
-
@Mahdi_Nine - see my answer on how to do it from the database side. Then when you apply `ddl.selectedindex=-1;` it will work properly as you want. – JonH Aug 09 '12 at 16:29
-
Please see: http://stackoverflow.com/questions/267064/asp-net-add-blank-item-at-top-of-dropdownlist – bugnuker Aug 09 '12 at 17:04
2 Answers
3
Add an empty unbound item
<asp:DropDownList runat="server" AppendDataBoundItems="true">
<asp:ListItem Selected="true" Text="" Value="" />
</asp:DropDownList>

Amiram Korach
- 13,056
- 3
- 28
- 30
0
You can add an empty data listitem from your backend or directly from the drop down list:
From the db side:
SELECT
1 as SortOrder,
DataValueField,
DataTextField
FROM
YourTable
UNION ALL
SELECT
0 As SortOrder, --this ensures the empty item is at the top of the list
0 As DataValueField,
'' As DataTextField --or 'Select One'
Order By
SortOrder,
Value
Or directly in as a list item
<asp:ListItem Selected="True" Text="Select One" Value="0" AppendDataBoundItems="true" />

JonH
- 32,732
- 12
- 87
- 145
-
Changing your data source just for a UI problem is not a good idea. Also, if you add the item manually, you must set `AppendDataBoundItems="true"` – Amiram Korach Aug 10 '12 at 05:40
-
@AmiramKorach this is a wiki type site - in the future feel free to edit. – JonH Aug 10 '12 at 12:26