Non-Selectable ListItems
Example 1: The Basics
Select a vacation
This page demonstrates the basics of how to insert a non-selectable ListItem into an ASP.NET control (in this case, a DropDownList; but the technique will work for any ASP.NET control that uses ListItem controls for its data items).
Page Code
<asp:DropDownList runat="server" ID="ddlVacation" AutoPostBack="true" OnSelectedIndexChanged="ddlVacation_postBack">
<asp:ListItem Text="-- Sunny Island -- " Value="" />
<asp:ListItem Value="Aruba" />
<asp:ListItem Value="Hawaii" />
<asp:ListItem Value="Bermuda" />
<asp:ListItem Text="-- Snowy Getaway --" Value="" />
<asp:ListItem Value="Vail, CO" />
<asp:ListItem Value="Nome, AK" />
<asp:ListItem Value="Greenland" />
<asp:ListItem Value="The North Pole" />
<asp:ListItem Value="Antartica" />
<asp:ListItem Text="-- Big Adventure --" Value="" />
<asp:ListItem Value="Mt. Everest" />
<asp:ListItem Value="The Amazon River" />
</asp:DropDownList>
<asp:Label runat="server" ID="lblMessage" Text="Select a vacation" />
Code Behind
Sub ddlVacation_postBack(ByVal Sender As Object, ByVal E As EventArgs)
If ddlVacation.SelectedValue = String.Empty Then
lblMessage.Text = "Sorry, that's not an option. Please choose again."
lblMessage.Font.Bold = True
lblMessage.ForeColor = Drawing.Color.Red
Else
lblMessage.Text = "I would love to go to <strong>" & ddlVacation.SelectedValue & "</strong>!"
lblMessage.Font.Bold = False
lblMessage.ForeColor = Drawing.Color.Black
End If
End Sub