Dependent DropDownLists

S

stephen

I am having a problem with my datagrid. I have 2 edit
columns containing DropDownLists, one dependent on
another. Both lists are being populated from a sql
database.

Essentially, the first DropDownList contains a list of
colors (i.e. Red, Blue, Green, etc) with the second
DropDownList containing different shades available for
the selected color (i.e. if Red is selected in the first
DropDownList then the second DropDownList should contain
Scarlet, Blood, Bright, Dark, etc).

I cannot find a way to populate the second DropDownList
each time the user changes their selection in the first
DropDownList.
 
N

Naveen

Hi Stephen,

You can implemented this functionality like this :

1. Add an handler for OnSelectedIndexChanged event of your First Drop Down
List.
i.e OnColorChange(ByVal sender As Object, ByVal e As EventArgs)
2. In the Event Handler :
'Get the First DropDown
Dim colorDropDown As DropDownList = CType(sender, DropDownList)
Dim selectedColorID As String = colorDropDown.SelectedItem.Value

'Get the Second DropDown
Dim item As DataGridItem = CType(colorDropDown.Parent.Parent, DataGridItem)
Dim ShadesDropDown As DropDownList = CType(item.FindControl("ddlShades"),
DropDownList)

'Get the Data from DB or Session to populate Second DropDown based on the
Item selected in First Dropdown

dtShades = CType(Session("shades"), DataTable)
dvShades = dtShades.DefaultView
Me.dvShades.RowFilter = String.Empty
If Not selectedColorID = "-1" Then
Me.dvShades.RowFilter = "ColorID ='" & selectedColorID & "'"
End If

With ShadesDropDown
.DataSource = dvShades
.DataValueField = "ShadeId"
.DataTextField = "ShadeName"
.DataBind()
End With

Hope It helps :)
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top