467,118 Members | 907 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,118 developers. It's quick & easy.

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

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

Jun 7 '06 #1
  • viewed: 9471
Share:
2 Replies
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.ConnectionStrin gs("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
--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)
"Sam" <gc*****@yahoo.com> escreveu na mensagem
news:11*********************@f6g2000cwb.googlegrou ps.com...
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

Jun 7 '06 #2
Sam
Thank you Bruno for your help. It works.

Regards

- Sam
Bruno Alexandre wrote:
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.ConnectionStrin gs("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
--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)
"Sam" <gc*****@yahoo.com> escreveu na mensagem
news:11*********************@f6g2000cwb.googlegrou ps.com...
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


Jun 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by =?Utf-8?B?Y2hlY2tyYWlzZXJAY29tbXVuaXR5Lm5vc3BhbQ== | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.