Print Page | Close Window

Dependent Dropdownlist boxes

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=16533
Printed Date: 29 March 2026 at 7:01pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Dependent Dropdownlist boxes
Posted By: Misty
Subject: Dependent Dropdownlist boxes
Date Posted: 13 September 2005 at 1:54am

I have a form with two drop down boxes. The first one, dropCounty is fine. But I'd like for the second one, dropCity to depend on dropCounty for the value. I would like for the dropCity box to show only cities that are for the certain county that was chosen. I would like for the second drop down box to be hidden until the person has chosen a county.  Can someone please help me with the code for this?


Here's my code:

        '----------------------------------------------------------------

        ' name: BindCountyList() - Fine

        '----------------------------------------------------------------

        Sub BindCountyList()

   

    Dim strConnect As String   

    Dim conCounty As New System.Data.SqlClient.SQLConnection

    Dim cmdSelect As New System.Data.SqlClient.SQLCommand

    Dim dtrCounty As System.Data.SqlClient.SqlDataReader

   

    'Get connection string from Web.Config

    strConnect = ConfigurationSettings.AppSettings("ConnectionString")

 

    conCounty = New System.Data.SqlClient.SQLConnection(strConnect)

    conCounty.Open()

    cmdSelect = New System.Data.SqlClient.SQLCommand( "Select CountyID, County From County Order by County", conCounty)

    dtrCounty = cmdSelect.ExecuteReader()

 

    dropCounty.DataSource = dtrCounty

    dropCounty.DataValueField = "CountyID"

    dropCounty.DataTextField = "County"

    dropCounty.DataBind()

   

     'Add and select a "Select County" option for the DDL

     dropCounty.Items.Insert(0, new ListItem("Select County", ""))

     dropCounty.SelectedIndex = 0

 

    dtrCounty.Close()

  &nbs




Replies:
Posted By: michael
Date Posted: 15 September 2005 at 9:36am
Not feeling like going through all your code but essentially, have ddl1 for countries and ddl2 for cities
with visible=false and ddl1.AutoPostBack=True
 
[code]
   

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then

            BindCountryList()

        End If

    End Sub

 

    Private Sub BindCountryList()

        Dim strConnect As String

        Dim conCounty As New System.Data.SqlClient.SqlConnection

        Dim cmdSelect As New System.Data.SqlClient.SqlCommand

        Dim dtrCounty As System.Data.SqlClient.SqlDataReader

        'Get connection string from Web.Config

        strConnect = ConfigurationSettings.AppSettings("ConnectionString")

        conCounty = New System.Data.SqlClient.SqlConnection(strConnect)

        conCounty.Open()

        cmdSelect = New System.Data.SqlClient.SqlCommand("Select CountyID, County From County Order by County", conCounty)

        dtrCounty = cmdSelect.ExecuteReader()

        dropCounty.DataSource = dtrCounty

        dropCounty.DataValueField = "CountyID"

        dropCounty.DataTextField = "County"

        dropCounty.DataBind()

        dtrCounty.Close()

        conCounty.Close()

    End Sub

 

    Private Sub dropCounty_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dropCounty.SelectedIndexChanged

        dropCity.Items.Clear()

        Dim strConnect As String

        Dim conCity As New System.Data.SqlClient.SqlConnection

        Dim cmdSelect As New System.Data.SqlClient.SqlCommand



-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net