Non-Selectable ListItems
Example 2: Periodic Separators In A DataBound Control

Select a town from the list and click Get Postal Code.

This is a demonstration of programmatically inserting periodic, non-selectable ListItem "separators" into to a databound DropDownList control. The purpose is to break up long lists of options into more visible groups.

Page Code

            <asp:DropDownList runat="server" ID="ddlMyList" />
            <asp:Label runat="server" ID="lblStatus" Text="Select a town from the list and click Get Postal Code." />
            <br />
            <asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_onClick" Text="Get Postal Code" />
        

Code Behind

            Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load
                If Not Page.IsPostBack Then
                    'bind the dropdownlist
                    ddlMyList_dataBind()
                End If
            End Sub

            Sub ddlMyList_dataBind()
                'Subroutine that binds listitems to dropdownlist
                'This requires the System.Data and System.Data.SqlClient namespaces

                'Open connection to database; declare SQL statement
                'You really should use parameterized stored procedures, but we'll do this as straight SQL
                Dim objConn As New SqlConnection("my connection string")
                Dim objCmd As New SqlCommand("SELECT * FROM states_table ORDER BY state_name ASC", objConn)
                objCmd.CommandType = CommandType.Text

                'We need a generic loop counter
                Dim I As Integer = 0

                Try
                    'connect to db
                    objConn.Open()
                    'create a data reader and populate it with SQL statement results
                    Dim objReader As SqlDataReader = objCmd.ExecuteReader()

                    'if the reader has records, let's bind the results
                    If objReader.HasRows() Then
                        'go through all items in the recordset
                        'we'll read the items one by one
                        Do While objReader.Read()
                            'add a heading at the top of the list and every 10 additional items
                            If I Mod 10 = 0 And I < 50 Then
                                ddlMyList.Items.Insert(I, New ListItem("--------------------", String.Empty))
                            Else
                                'add the current data reader item
                                ddlMyList.Items.Insert(I, New ListItem(objReader("state_name"), objReader("state_code")))
                            End If
                            'increment the loop counter
                            I += 1
                        Loop
                    Else
                        'report that there are no items to display in the list
                        ' and disable the dropdownlist
                        lblStatus.Text = "There are no records for this item"
                        ddlMyList.Enabled = False
                    End If

                    'close the connection
                    objConn.Close()

                Catch ex As Exception
                    'if there is an error, report it
                    ' and disable the dropdownlist
                    lblStatus.Text = ex.Message
                    ddlMyList.Enabled = False
                End Try

                'clean up the database objects
                objCmd.Dispose()
                objConn.Dispose()
            End Sub

            Sub btnSubmit_onClick(ByVal Sender As Object, ByVal E As EventArgs) Handles btnSubmit.Click
                'report when a non-selectable item has been selected
                'non-selectable items have no values
                If String.IsNullOrEmpty(ddlMyList.SelectedValue) Then
                    lblStatus.Text = "You cannot select this item. Please select a different item."
                    lblStatus.ForeColor = Drawing.Color.Red
                    lblStatus.Font.Bold = True
                Else
                    lblStatus.Text = "Postal code: " & ddlMyList.SelectedValue
                    lblStatus.ForeColor = Drawing.Color.Black
                    lblStatus.Font.Bold = False
                End If
            End Sub