我有一个Web更新表单,其中每个控件都是同一个表的属性。它应该以这种方式工作: 每当我从主(第一个)下拉列表中选择一个值时,应该运行查询获取所有字段(属性)并根据所选值填充其他控件。如何选择已从数据库返回的DropDownList的值
事件代码:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim objModel As New ModelDAO ' the data access class taht contains search method
Dim myobject = objModel.searchObject(DropDownList1.Text)
TextBox1.Text = myobject.Property2
DropDownList2.SelectedValue = myobject.Property3 'what's wrong here?
End Sub
控制:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource1" DataTextField="MODEL" DataValueField="MODEL"
AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="-Select-" Value="" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="0">-Select-</asp:ListItem>
<asp:ListItem>EDS</asp:ListItem>
<asp:ListItem>BDS</asp:ListItem>
</asp:DropDownList>
它的工作原理除了第二DropDownList的,我不知道如何更改选定的价值,我想这:
DropDownList2.Text = myobject.Property3
和这个:
DropDownList2.SelectedValue = myobject.Property3
但是在这两种情况下,dropdownlist2都不显示所选项目。
注:由于textbox.text确实得到正确的值,我不认为搜索方法有任何问题,这就是为什么我没有发布它。
为什么它不能这样工作?设置'SelectedValue'与调用'ClearSelection'后循环项目并将第一个匹配项目的'Selected'属性设置为'True'相同。 –
以我的经验,我从来没有选择过任何价值的作品。这只是对我而言没有效果。 –
谢谢我转换成VB和它的工作中:Dim我为整数= 0 对于每个列表项在DropDownList1.Items 如果DropDownList3.SelectedItem.Text = myobject.property然后 DropDownList3.SelectedIndex = I 退出对于 结束如果 我= i + 1 Next – phalanx