根据另一个下拉列表更改下拉列表的绑定 [英] Change binding of dropdown list based on another dropdownlist
问题描述
我有2个下拉列表.一个是手动填充的.另一个我想绑定到另一个表,具体取决于第一个表中选择的值.
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.
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 =紧急联系人">
< asp:ListItem Text ="Associates">
< asp:DropDownList ID ="ContactRelationship" runat ="server" Width ="200px">
我知道代码是野蛮的,找到解决方案后,我会清理它.
我正在使用asp.net 4.0,Linq to SQL和Sql Server Express 2008r2.
尝试更改ddl1中的值后出现错误.在我看来,ddl2将不会绑定到另一个表,除非它以某种方式未绑定?
我真正需要的只是向正确的方向推进.谢谢您的帮助.
<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.
推荐答案
jfclrak27,
浏览以下链接,
点击我 [
Hi jfclrak27,
Go through the following link,
Click me[^]
我将对此进行研究.谢谢你.昨晚我研究了问题,并提出了替代解决方案.我意识到我并不需要绑定ddl到表,只需使用linq将其读入数据集然后绑定到该表即可.
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()
这对我来说很棒.不知道为什么我全都包裹在车轴上,试图重塑车轮.我正在设计Web应用程序中的向导.
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.
这篇关于根据另一个下拉列表更改下拉列表的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!