问题描述
你好,
我想在同一下拉列表中显示州和相应的城市.
喜欢
(1)北方邦-------国家名称
(a)拉克瑙-------城市名
(b)加兹阿巴德-------城市名
(c)坎普尔-------城市名
(2)哈里亚纳邦-------州名
(a)古尔冈-------城市名
(b)rohtak -------城市名
(c)panipat -------城市名
等等....
所有这些都在同一个下拉列表中.
首先是州名,然后是城市,然后是州名和其城市.
我已经使用了两个下拉菜单,并且工作正常,但是我需要在一个下拉菜单中进行展示.
谢谢
Hello,
i want to show the state and corresponding cities in the same drop down.
Like
(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
推荐答案
<select>
<optgroup label="Uttar pradesh">
<option value="Lucknow">Lucknow</option>
<option value="Ghaziabad">Ghaziabad</option>
<option value="Kanpur">Kanpur</option>
</optgroup>
<optgroup label="Haryana">
<option value="gurgaon">gurgaon</option>
<option value="rohtak">rohtak</option>
<option value="panipat">panipat</option>
</optgroup>
</select>
请参见来自W3Schools的示例 [ ^ ]
和参考页: W3Schoools选择标签参考 [ ^ ]
希望这会有所帮助,
Ed:)
See an Example from W3Schools[^]
and the reference page: W3Schoools Select Tag Reference[^]
Hope this helps,
Ed :)
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
dropdownlist.Items.Add(pair.Key);
foreach(var city in pair.Value)
{
//Add cityname
dropdownlist.Items.Add(city);
}
}
</string>
此致
<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
BindDropdown()
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
Me.DropDownList1.Items.Add(li)
Next
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)
Next
Return result
End Function
Public ReadOnly Property Value() As Integer
Get
Return 1
End Get
End Property
Public ReadOnly Property Prop1() As String
Get
Return "Value1"
End Get
End Property
Public ReadOnly Property Prop2() As String
Get
Return "Value2"
End Get
End Property
End Class
希望能帮助到你.
祝你好运. :)
-chaosgray-
hope it helps.
goodluck. :)
-chaosgray-
这篇关于同一下拉菜单中的州和城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!