Click here to Skip to main content
16,005,121 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have 2 drop-down lists. One is populated manually. The other I would like to bind to a different table depending on the value selected in the first.

VB
Protected Sub ContactType_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ContactType.SelectedIndexChanged
    Dim dc As New DataClassesDataContext()
    If ContactType.SelectedItem.Text = "Company" Then
        ContactRelationship.DataSource = dc.CompanyTypes
        ContactRelationship.DataTextField = "CompanyType"
        ContactRelationship.DataValueField = "CompanyTypeID"
        ContactRelationship.DataBind()
    ElseIf ContactType.SelectedItem.Text = "Employee" Then
        ContactRelationship.DataSource = dc.EmployeeTypes
        ContactRelationship.DataTextField = "EmployeeType"
        ContactRelationship.DataValueField = "EmployeeTypeID"
        ContactRelationship.DataBind()
    End If
End Sub




<asp:DropDownList ID="ContactType" runat="server" Width="200px" AutoPostBack="True">
<asp:ListItem Text="Company">
<asp:ListItem Text="Employee">
<asp:ListItem Text="Emergency Contact">
<asp:ListItem Text="Associates">



<asp:DropDownList ID="ContactRelationship" runat="server" Width="200px">



I know the code is barbaric, I'll clean it up after I find the solution.
I'm using asp.net 4.0, Linq to SQL, and Sql Server express 2008r2.

I'm getting an error after I try to change value in ddl1. It seem to me that ddl2 will not bind to another table until it is somehow unbound?

All I really need is a push in the right direction. Thank you for your help.
Posted
Comments
hitech_s 10-Sep-11 1:13am    
my suggestion is put your dropdownlists inside update panels it will help u

Hi jfclrak27,
Go through the following link,
Click me[^]
 
Share this answer
 
I will look into that. Thank you. I worked on problem last night and came up with an alternate solution. I realized I didn't really need to bind ddl to table, just use linq to read it into a data set then bind to that.

If ContactType.SelectedItem.Text = "Company" Then
         Dim Types = From t In dc.CompanyTypes
             Select t.CompanyType
         ContactRelationship.DataSource = Types
         ContactRelationship.DataBind()


This is working great for me. Don't know why I got all wrapped around the axle trying to reinvent the wheel. I'm working on designing a wizard in a web app.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900