Click to See Complete Forum and Search --> : Dependent Dropdown Lists


iceman316
03-21-2008, 12:35 PM
Hi,

Im using ASP .NET 1.1 ->Moving to 2.0 and need some help with dependent dropdown lists. I have comboboxes for state,city & area.Based on state,city is populated and the area combobox is populated based on state & city values selected. Im using the autopostback property for doing this,but its not providing any consistent results(I dont know why).If anyone could explain why,it would be of great help.

Im using an ASP .NET with VB .NET codebehind & Forms Authentication.

I am a newbie as far as AJAX is concerned.I was thinking of doing the same thing using Javascript,but I wasnt too sure how to go about doing that too.However, AJAX seems to be a good clean way to get things done. Any ideas/nudges in the right direction would be a great help.Ive pasted my code for the population of one of the comboboxes so that you will get some idea of what im implementing. Im using an SQL server 2000 backend.

Thanks,
Ray.

Private Sub DropDownList9_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList9.SelectedIndexChanged
'Populate The Area List
Dim a, b As String
a = DropDownList8.SelectedItem.ToString
b = DropDownList9.SelectedItem.ToString
'//declare connection string
Dim cnString As String = ("Data Source=127.0.0.1; Initial Catalog=Administration" + "User Id=sa;Password=dbpassword")

' declare Connection, command and other related objects
Dim conGetData As New SqlConnection(cnString)
Dim cmdgetdata As New SqlCommand
Dim drgetdata As SqlDataReader

Try

'open connection
conGetData.Open()

'prepare connection object to get the data through
'reader and populate into dataset
cmdgetdata.CommandType = CommandType.Text
cmdgetdata.Connection = conGetData
cmdgetdata.CommandText = "SELECT cityname FROM citylist where StateName=@name and cityname=@name2"
cmdgetdata.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = a
cmdgetdata.Parameters.Add("@name2", SqlDbType.VarChar, 50).Value = b
'read data from command object
drgetdata = cmdgetdata.ExecuteReader()

If drgetdata.HasRows = True Then
While drgetdata.Read()
DropDownList10.Items.Add(drgetdata("AreaName").ToString())
End While
Else
'close reader and connection
drgetdata.Close()
conGetData.Close()
End If
Catch ex As Exception
Response.Write(ex.ToString)
Finally
'check if connection is still open then attempt to close it
If conGetData.State = ConnectionState.Open Then
conGetData.Close()
End If
End Try
End Sub

iceman316
03-25-2008, 08:16 AM
Never mind . . . .got it using AJAX.

Here (http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Walkthrough/CCDWithDB.aspx)

Hack
03-25-2008, 01:55 PM
Thanks for the link! :)