I have 4 dynamic drop down lists(Region, Country, State, City) the
Country depends on the region selected, the State depends on the
Country and the City depends on the States,all this information is in
SQL server, thed I add and item (DropDownList1.Items.Insert(0, "select
an item")to each dropdownlist so far this is working fine. The
problem that I have is that I need the country, state, and city
dropdownlists to be reset to the first list item the one that I insert
when the region changes, and also I need to add a value 0 to this
item.
I am new to VB.NET maybe this is not the best approch, but with my
knowledge, that is what I can do.
I will appreciate any help, Thanks.
This is the code :
Namespace="Microsoft.Saturn.Framework.Web.UI"
Assembly="Microsoft.Saturn.Framework, Version=0.5.464.0,
Culture=neutral, PublicKeyToken=6f763c9966660626" %>
<script runat="server">
' Insert page code here
'
Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
DropDownList1.DataValueField = "id"
DropDownList1.DataTextField = "region_name"
DropDownList1.DataSource = Getregion()
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, "select an item")
End If
End Sub
Function Getregion() As System.Data.DataSet
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='comida'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection =
New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT
[regiones].[ID],[regiones].[region_name] FROM [regiones] order by
region_name"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter =
New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As System.Data.DataSet = New
System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
Function Getpais() As System.Data.DataSet
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='comida'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection =
New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT
[paises].[ID],[paises].[description] FROM [paises] where
([paises].[region] = " & DropDownList1.selectedItem.Value &")" &
"order by description"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter =
New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As System.Data.DataSet = New
System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
Function Getestado() As System.Data.DataSet
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='comida'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection =
New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT
[estados].[ID],[estados].[estado] FROM [estados] where
([estados].[pais] = " & DropDownList2.selectedItem.Value &")" & "order
by estado"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter =
New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As System.Data.DataSet = New
System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
Function Getciudad() As System.Data.DataSet
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='comida'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection =
New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT
[ciudades].[ID],[ciudades].[ciudad] FROM [ciudades] where
([ciudades].[estado] = " & DropDownList3.selectedItem.Value &")" &
"order by ciudad"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter =
New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As System.Data.DataSet = New
System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
Sub DropDownList1_SelectedIndexChanged(sender As Object, e As
EventArgs)
DropDownList2.DataValueField = "id"
DropDownList2.DataTextField = "description"
DropDownList2.DataSource = Getpais()
DropDownList2.DataBind()
DropDownList2.Items.Insert(0, "select an item")
End Sub
Sub DropDownList2_SelectedIndexChanged(sender As Object, e As
EventArgs)
DropDownList3.DataValueField = "id"
DropDownList3.DataTextField = "estado"
DropDownList3.DataSource = Getestado()
DropDownList3.DataBind()
DropDownList3.Items.Insert(0, "select an item")
End Sub
Sub DropDownList3_SelectedIndexChanged(sender As Object, e As
EventArgs)
DropDownList4.DataValueField = "id"
DropDownList4.DataTextField = "ciudad"
DropDownList4.DataSource = Getciudad()
DropDownList4.DataBind()
DropDownList4.Items.Insert(0, "select an item")
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DropDownList id="DropDownList1" runat="server"
Height="30px" Width="150px" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<br />
<br />
<asp:DropDownList id="DropDownList2" runat="server"
Height="30px" Width="150px" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"></asp:DropDownList>
<br />
<br />
<asp:DropDownList id="DropDownList3" runat="server"
Height="30px" Width="150px" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged"></asp:DropDownList>
<br />
<br />
<asp:DropDownList id="DropDownList4" runat="server"
Height="30px" Width="150px" AutoPostBack="True"></asp:DropDownList>
</form>
</body>
</html>