Click here to Skip to main content
15,064,140 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

i want to show the state and corresponding cities in the same drop down.

(1) Uttar pradesh ------- Statename
(a) Lucknow ------- Cityname
(b) Ghaziabad ------- Cityname
(c) Kanpur ------- Cityname

(2) Haryana ------- Statename
(a) gurgaon ------- cityname
(b) rohtak ------- cityname
(c) panipat ------- cityname

and so on....

This all come in the same drop down list.
First statename then its cities and again statename and its cities.

I have used two drop down and its working good but i need to show it in one drop down.

Thank you

Try using <optgroup></optgroup> tags in a select control then convert it to ASP.Net. The code for your example would be the following:

  <optgroup label="Uttar pradesh">
    <option value="Lucknow">Lucknow</option>
    <option value="Ghaziabad">Ghaziabad</option>
    <option value="Kanpur">Kanpur</option>
  <optgroup label="Haryana">
    <option value="gurgaon">gurgaon</option>
    <option value="rohtak">rohtak</option>
    <option value="panipat">panipat</option>

See an Example from W3Schools[^]
and the reference page: W3Schoools Select Tag Reference[^]

Hope this helps,

Ed :)
Toniyo Jackson 7-Jun-11 2:28am
Good answer. My +5
thatraja 7-Jun-11 3:42am
Perfect, 5!
Sandeep Mewara 7-Jun-11 12:10pm
My 5!
you have just to add the statenames/cities in the corresponding order into the DropDownList. Initially you better store them in the Dictionary with Key (statename) and Value (list of cities). Once done, iterate over the dictionary and add the corresponding items.
Dictionary<string,>> dictionary = new Dictionary<String, List<String>>();
//Add items to the dictionary
dictionary.Add("Statename1", new List<string>(new [] {"city1", "city2"}));

//Iterate over the dictionary
foreach(var pair in dictionary)
    //Add statename
    foreach(var city in pair.Value)
        //Add cityname

thatraja 7-Jun-11 3:43am
Also this, 5!

try this one.

<pre lang="vb">Partial Class _Default
  Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub BindDropdown()
        'by manually adding the listitems, we can easily alter them as they are added
        For Each dataItem As TestData In TestData.GetTestData
            Dim li As New ListItem
            li.Value = dataItem.Value.ToString
            'we can easily combine multpile properties from our "datasource" to get a display value
            li.Text = dataItem.Prop1 & " " & dataItem.Prop2
    End Sub

End Class

'this Class is just something to give us some test data for our example
Public Class TestData
  Public Shared Function GetTestData() As ArrayList
    Dim result As New ArrayList
    For x As Integer = 1 To 25
      result.Add(New TestData)
    Return result
    End Function
    Public ReadOnly Property Value() As Integer
            Return 1
        End Get
    End Property
    Public ReadOnly Property Prop1() As String
            Return "Value1"
        End Get
    End Property
    Public ReadOnly Property Prop2() As String
            Return "Value2"
        End Get
    End Property
End Class

hope it helps.
goodluck. :)


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900