New to ASP.Net, Cascading DropDown List in Detail View...Please help

S

Sam

A) Destination Table with 4 Fields.

1) Last Name
2) First Name
3) State
4) Zip Code.


B) Look up table State/Zip Code with 2 Fields

1) State
2) Zip Code.



I created a DetailView Form in Asp.net to Insert/Update Destination
Table. Destination Table's State and Zip Code Fields are converted from
Bound Column to Item/Edit/Insert Template, Created 2 Drop Down Lists,
DDLState and DDLZIP. I am using '<%# Bind(...)%>' for both of them.
Also, I do have 2 SQLDatasource for DDL's (i.e. SqlState for DDLState
and SqlZip for DDLZIP).

My Intention is very simple:

When user select State Name from DDLState DropDownList List (example:
NJ), in DDLZIP Dropdown list it must show only Zip Codes from that
State. In order to Change the Value of DDLZIP, users must Change the
Value of DDLState. Basically DDLZIP is Child/Dependant of DDLState. I
did correctly setup proper reference to DDLState in SelectedParameters,
Control Parameters, PropertyName in SqlZip (SqlDataSource for DDLZIP).

This Cascading dropdown logic does work Fine in a Form without a
DetailView. I tested it. But I am having hard time to implement this
within Detail View.


Any Complete Example (i.e. Form with Subs) will be highly appreciated.
I will be glad if this example is based on NorthWind Database.

Thanks a lot for your time and efforts.

- Sam
 
B

Bruno Alexandre

well,

the first Bind to the 2nd DDL will be returning just the ZIP Code that
actually is in the database unless you choose otherwise.

my opinion on the matter is:

1 DDL have a function on OnSelectedIndexChanged that goes to the DB and gett
all the zip codes for that State and Populate the 2nd DDL, that's simple.

like:

myFisrtDDL_SelectedIndexChanged( ... )
mySecondDDL.DataSource = populateSecondDD( myFirstDDL.SelectedValue )
mySecondDDLDataBind()

Function populateSecondDD( state as String ) as ArrayList

' make the connection to the Database
Dim aArray as new ArrayList
Dim dReader As SqlDataReader
Dim Connection As SqlConnection
Connection = New _
SqlConnection(ConfigurationManager.ConnectionStrings("ConnString").ConnectionString)
Dim command As SqlCommand
command = New SqlCommand()
command.Connection = Connection
command.CommandType = Data.CommandType.Text
command.CommandText = "SELECT zipCodes FROM table WHERE State = @state"
Connection.Parameters.AddWithValue("@state", state)
Connection.Open()
myDataReader = Connection.Execute.Reader()

If myDataReader.HasRows Then
While myDataReader.Read()
aArray.Add( myDataReader.item("zipCodes"))
End While
End If
myDataReader.Close()
Connection.Close()
Return aArray
End Function


and of course, on the GridView_RowCreated statment you just do the same

GridView_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowState = DataControlRowState.Edit
CType(e.Row.FindControl("mySecondDDL"), DropDownList).DataSource =
populateSecondDDL( _
CType(e.Row.FindControl("myFirstDDL"),
DropDownList).SelectedValue
CType(e.Row.FindControl("mySecondDDL"), DropDownList).DataBind()
End If
End if
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top