如何选择已经从数据库返回的一个DropDownList的值 [英] How to select a value of a DropDownList that has been returned from a database
问题描述
我有一个网页更新表单,每一个控制是同桌的属性。它应该是这样的:
每当我从主(第一批)的DropDownList值,查询应运行让所有的字段(属性)和填充根据我选择的值的其他控件。
I have a web update form where every control is a property of the same table. It should work this way: Whenever I select a value from the main(the first) dropdownlist, a query should be run getting all of the fields(properties) and filling the other controls depending of the value I selected.
事件code:
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的,我不知道如何改变所选的值,我想这样的:
It works except the second DropDownList, I don't know how to change the selected value, I tried this:
DropDownList2.Text = myobject.Property3
和这样的:
DropDownList2.SelectedValue = myobject.Property3
但是,在这两种情况下dropdownlist2没有显示出选定的项目。
But in both cases dropdownlist2 shows no selected item.
请注意:由于textbox.text确实让我不认为搜索方法有什么问题,正确的价值,这就是为什么我没有张贴
NOTE: Since textbox.text does get the right value I don't think search method has any problem, that's why I'm not posting it.
推荐答案
它不工作的方式很遗憾。我给你伪code,因为我是一个C#呃,但是这是我常做的:
It doesn't work that way unfortunately. I'll give you pseudocode since I'm a C#er, but this is how I normally do it:
Dim int i = 0
ForEach Item in DDL.Items
If Item.Text = databaseResult
DDL.SelectedIndex = i
ExitLoop
EndIf
i = i + 1
EndForEach
下面是从下面的评论跟帖工作code:
Here is the working code from the comment thread below:
Dim i As Integer = 0
For Each listitem In DropDownList1.Items
If DropDownList3.SelectedItem.Text = myobject.property Then
DropDownList3.SelectedIndex = i
Exit For
End If
i = i + 1
Next
这篇关于如何选择已经从数据库返回的一个DropDownList的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!