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

Discussion in 'ASP .Net Datagrid Control' started by Sam, Jun 7, 2006.

  1. Sam

    Sam Guest

    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
     
    Sam, Jun 7, 2006
    #1
    1. Advertising

  2. 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


    --

    Bruno Alexandre
    (a Portuguese in Københanv, Danmark)


    "Sam" <> escreveu na mensagem
    news:...
    > 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
    >
     
    Bruno Alexandre, Jun 7, 2006
    #2
    1. Advertising

  3. Sam

    Sam Guest

    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.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
    >
    >
    > --
    >
    > Bruno Alexandre
    > (a Portuguese in Københanv, Danmark)
    >
    >
    > "Sam" <> escreveu na mensagem
    > news:...
    > > 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
    > >
     
    Sam, Jun 9, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luqman
    Replies:
    2
    Views:
    3,821
    Luqman
    Feb 1, 2006
  2. Sam
    Replies:
    2
    Views:
    9,495
  3. acadam
    Replies:
    0
    Views:
    760
    acadam
    Dec 27, 2006
  4. Frederik Borg

    ASp.net data binding: Master-detail-detail

    Frederik Borg, Jun 9, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    362
    Frederik Borg
    Jun 9, 2006
  5. Raymond Chiu

    Ajax Cascading Dropdown List

    Raymond Chiu, Apr 8, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    186
    Raymond Chiu
    Apr 8, 2008
Loading...

Share This Page