Parent-Child DropDownList Controls
In ASP.NET Web Forms

Example 2: Using Code Behind And CompareValidator Controls

This demo uses code behind to bind data to the parent and child DropDownLists, as well as the detail GridView. It also uses CompareValidators to ensure a valid option is chosen from the DropDownMenus before they are bound.

Select A State:
Select A Town:

Page Code

            <asp:Label runat="server" ID="lblParent" Text="Select A State: " />
            <asp:DropDownList runat="server" ID="ddlParent" AutoPostBack="true">
                <asp:ListItem Text="Select A State ..." Value="0" />
            </asp:DropDownList>
            <asp:CompareValidator runat="server" ID="cvParent" ControlToValidate="ddlParent" ValueToCompare="0" Operator="NotEqual" ErrorMessage="Please select a valid option" Display="Dynamic" />
            <br />
            <asp:Label runat="server" ID="lblChild" Text="Select A Town: " />
            <asp:DropDownList runat="server" ID="ddlChild" AutoPostBack="true">
                <asp:ListItem Text="Select A State First" Value="0" />
            </asp:DropDownList>
            <asp:CompareValidator runat="server" ID="cvChild" ControlToValidate="ddlChild" ValueToCompare="0" Operator="NotEqual" ErrorMessage="Please select a valid option" Display="Dynamic" />
            <br />
            <br />
            <asp:GridView runat="server" ID="gvDetails" CellPadding="5" AutoGenerateColumns="false">
                <AlternatingRowStyle BackColor="LightGray" />
                <HeaderStyle BackColor="LightYellow" Font-Bold="True" HorizontalAlign="Center" />
                <Columns>
                    <asp:BoundField HeaderText="City" DataField="CityName" />
                    <asp:BoundField HeaderText="State" DataField="StateInitials" />
                    <asp:BoundField HeaderText="ZIP Code" DataField="ZIPCode" />
                    <asp:BoundField HeaderText="Latitide" DataField="CityLatitude" />
                    <asp:BoundField HeaderText="Longitude" DataField="CityLongitude" />
                </Columns>
            </asp:GridView>
        

Code Behind

            Sub Page_Load(Sender As Object, E As EventArgs) Handles Me.Load
	            If Not Page.IsPostBack Then
		            ddlParent_dataBind()
	            End If
            End Sub

            Sub ddlParent_dataBind()
	            Dim objConn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
	            Dim objCmd As New SqlCommand("GetStates", objConn)
	            objCmd.CommandType = CommandType.StoredProcedure

	            Dim objReader As SqlDataReader

	            objConn.Open()
	            objReader = objCmd.ExecuteReader()

	            If Not objReader.HasRows Then
		            ddlParent.Items.Clear()
		            ddlParent.Items.Add("No records found.")
		            ddlParent.Enabled = False
	            Else
		            ddlChild.Enabled = True
		            ddlParent.DataSource = objReader
		            ddlParent.DataTextField = "StateName"
		            ddlParent.DataValueField = "StateCode"
		            ddlParent.DataBind()
		            ddlParent.Items.Insert(0, New ListItem("Select A State ...", "0"))
	            End If

	            objConn.Close()
	            objCmd.Dispose()
	            objConn.Dispose()
            End Sub

            Sub ddlChild_dataBind(strKey As String)
	            ddlChild.Items.Clear()

	            Dim objConn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
	            Dim objCmd As New SqlCommand("GetZIPCodes", objConn)
	            objCmd.CommandType = CommandType.StoredProcedure

	            objCmd.Parameters.Add(New SqlParameter("StateInitials", SqlDbType.Char, 2))
	            objCmd.Parameters("StateInitials").Value = strKey

	            Dim objReader As SqlDataReader

	            objConn.Open()
	            objReader = objCmd.ExecuteReader()

	            If Not objReader.HasRows Then
		            ddlChild.Items.Clear()
		            ddlChild.Items.Insert(0, "Error getting towns list from database")
	            Else
		            ddlChild.Enabled = True
		            ddlChild.DataSource = objReader
		            ddlChild.DataTextField = "CityName"
		            ddlChild.DataValueField = "ZIPCode"
		            ddlChild.DataBind()
		            ddlChild.Items.Insert(0, New ListItem("Select A Town ...", "0"))
	            End If

	            objConn.Close()
	            objCmd.Dispose()
	            objConn.Dispose()
            End Sub

            Sub gvDetails_dataBind(strKey As String)
	            Dim objConn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
	            Dim objCmd As New SqlCommand("GetCityDetails", objConn)
	            objCmd.CommandType = CommandType.StoredProcedure

	            objCmd.Parameters.Add(New SqlParameter("ZIPCode", SqlDbType.Char, 5))
	            objCmd.Parameters("ZIPCode").Value = strKey

	            Dim objReader As SqlDataReader

	            objConn.Open()
	            objReader = objCmd.ExecuteReader()

	            gvDetails.DataSource = objReader
	            gvDetails.DataBind()

	            objConn.Close()
	            objCmd.Dispose()
	            objConn.Dispose()
            End Sub

            Sub gvDetails_reset()
	            gvDetails.DataSource = Nothing
	            gvDetails.DataBind()
            End Sub

            Sub ddlParent_selectedIndexChanged(Sender As Object, E As EventArgs) Handles ddlParent.SelectedIndexChanged
	            cvParent.Validate()
	            If cvParent.IsValid Then
		            ddlChild_dataBind(ddlParent.SelectedValue)
		            ddlChild.SelectedIndex = 0
	            End If
	            gvDetails_reset()
            End Sub

            Sub ddlChild_selectedIndexChanged(Sender As Object, E As EventArgs) Handles ddlChild.SelectedIndexChanged
	            cvChild.Validate()
	            If cvChild.IsValid Then
		            gvDetails_dataBind(ddlChild.SelectedValue)
	            Else
		            gvDetails_reset()
	            End If
            End Sub