Dropdowns in webform

  • Thread starter Thread starter ReidarT
  • Start date Start date
R

ReidarT

I have 2 dropdownboxes in a web-form. The first one is filled with countries
when opening page.
The other one is filled with appropriate cities after selecting country. I
want the city-ddl to have a text before a country is selected like
"--city--".
I want the same on the country-dll after page is opened, like "--country--".
regards
reidarT
 
CityDropDown.Items.Insert (0, "--City--")
CountryDropDown.Items.Insert (0, "--Country--")

Remember you have to do this after you populated the drop-downs.

I have 2 dropdownboxes in a web-form. The first one is filled with countries
when opening page.
The other one is filled with appropriate cities after selecting country. I
want the city-ddl to have a text before a country is selected like
"--city--".
I want the same on the country-dll after page is opened, like "--country--".
regards
reidarT
 
Thanks
What I really wanted was:
When I select Armenia, there is only one city in the list Yerevan. On the
event ddlCity_SelectedIndexChanged I fill a datagrid with buildings in this
city. And when there is only one city there is no change on selection.
reidarT
 
Reidar,

I made this sample for you now. So test it well when you want to use it.

It uses a shared sub which should be dealed by all active sessions at that
moment in this application.

I hope this helps?

Cor

\\\
Private Sub DropDownList1_SelectedIndexChanged(ByVal _
sender As System.Object, ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
mycitys.dv.RowFilter = "Countries = '" & _
DropDownList1.SelectedValue & "'"
DropDownList2.DataSource = mycitys.dv
DropDownList2.DataTextField = "Cities"
DropDownList2.DataBind()
End Sub
Private Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DropDownList1.Items.Clear()
DropDownList1.Items.Add("France")
DropDownList1.Items.Add("Holland")
If mycitys.dv Is Nothing Then
myCitys.CreateDT()
End If
mycitys.dv.RowFilter = "Countries = 'France'"
DropDownList2.DataSource = mycitys.dv
DropDownList2.DataTextField = "Cities"
DropDownList2.DataBind()
End If
End Sub
End Class
Public Class myCitys
Public Shared dv As DataView
Public Shared Sub CreateDT()
Dim dt As New DataTable
dt.Columns.Add("Countries")
dt.Columns.Add("Cities")
dt.Rows.Add(dt.NewRow)
For i As Integer = 0 To 3
dt.Rows.Add(dt.NewRow)
Next
dt.Rows(0).ItemArray = New Object() _
{"France", "Paris"}
dt.Rows(1).ItemArray = New Object() _
{"France", "Lyon"}
dt.Rows(2).ItemArray = New Object() _
{"Holland", "Amsterdam"}
dt.Rows(3).ItemArray = New Object() _
{"Holland", "Rotterdam"}
dv = New DataView(dt)
End Sub
///
 
If you have only one in the DDL, it will be selected by default and there is
no option for selecting antoher item and hence no index change event. I
believe if you insert '--City--' and select a city, it should work.

Hope I understood your question correctly.

HTH.

Thanks
What I really wanted was:
When I select Armenia, there is only one city in the list Yerevan. On the
event ddlCity_SelectedIndexChanged I fill a datagrid with buildings in this
city. And when there is only one city there is no change on selection.
reidarT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top