I want to add a value to the drop down list that cannot be selected, like a title. Ex: I have a month drop down list. The very first item should be \"select month\" this sho
try this one
<asp:DropDownList ID="ddList" runat="server">
<asp:ListItem Value="">--Select Month--</asp:ListItem>
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">Feburary</asp:ListItem>
...
<asp:ListItem Value="12">December</asp:ListItem>
</asp:DropDownList>
Value should be empty for the default selected listitem, then it works fine
These are ALL great answers if you want to work that hard. But my guess is that you already have the items you want for the list coming from a databound element, and only want to add to the top of that list the "Hey, dude - pick one!" option. Assuming that is the case...
Here's the EASY Answer. And it ALWAYS works...
Now you will get a Drop Down with all of your data items in it, PRECEDED BY your "Select an Item" statement made in the manual item. Try giving it a default value if possible, this will eliminate any errors you may encounter. The default is Zero, so if zero is not a problem, then leave it alone, if zero IS a problem, replace the default zero in the item with something that will NOT crash your code.
And stop working so hard...that's what Visual Studio is for.
VB Code:
Dim ListItem1 As New ListItem()
ListItem1.Text = "put anything here"
ListItem1.Value = "0"
drpTag.DataBind()
drpTag.Items.Insert(0, ListItem1)
View:
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator" ControlToValidate="drpTag"
ValueToCompare="0">
</asp:CompareValidator>