vb.net 如何选择从数据库返回的 DropDownList 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17981920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:29:21  来源:igfitidea点击:

How to select a value of a DropDownList that has been returned from a database

asp.netvb.netlistobjectcombobox

提问by phalanx

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.

我有一个 Web 更新表单,其中每个控件都是同一个表的一个属性。它应该这样工作:每当我从主(第一个)下拉列表中选择一个值时,应该运行查询以获取所有字段(属性)并根据我选择的值填充其他控件。

Event 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

Controls:

控制:

<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>

It works except the second DropDownList, I don't know how to change the selected value, I tried this:

除了第二个 DropDownList 之外它都有效,我不知道如何更改选定的值,我试过这个:

DropDownList2.Text = myobject.Property3

and this:

和这个:

DropDownList2.SelectedValue = myobject.Property3

But in both cases dropdownlist2 shows no selected item.

但在这两种情况下,下拉列表 2 均未显示所选项目。

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.

注意:由于 textbox.text 确实获得了正确的值,我认为搜索方法没有任何问题,这就是我不发布它的原因。

回答by Garrison Neely

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:

不幸的是,它不能那样工作。因为我是 C#er,所以我会给你伪代码,但这是我通常的做法:

Dim int i = 0
ForEach Item in DDL.Items
    If Item.Text = databaseResult
        DDL.SelectedIndex = i
        ExitLoop
    EndIf
    i = i + 1
EndForEach

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

回答by SLaG

If VB is anything like C# (where I've had similar problems), then try

如果 VB 类似于 C#(我遇到过类似的问题),请尝试

DropDownList2.SelectedItem = myobject.Property3

Make sure the item you want selected is in the controls list of items otherwise it still won't work.

确保您要选择的项目在项目的控件列表中,否则它仍然无法工作。

回答by Mausimo

Here try this:

在这里试试这个:

        int i = 0;
        foreach (ListItem item in YourDropDownList.Items)
        {
            //Check it see if your item actually exists in the list
            //Could also be item.Text                
            if (item.Value.ToLower() == value)
            {
                item.Selected = true;
                YourDropDownList.SelectedIndex = i;
                break;
            }
            else
                item.Selected = false;

            i++;
        }

        if (YourDropDownList.SelectedIndex != -1)
        {                
            YourDropDownList_SelectedIndexChanged(YourDropDownList, EventArgs.Empty);
        }

Not sure if this will work, you can try it:

不确定这是否有效,您可以尝试一下:

    YourDropDownList.SelectedValue = myobject.Property3
    YourDropDownList_SelectedIndexChanged(YourDropDownList, EventArgs.Empty);

回答by Tez Wingfield

So you have this working apart from the showing of the selectedValue in the second dropdown?

所以除了在第二个下拉列表中显示 selectedValue 之外,你还有这个工作吗?

Remember when making comparsions it has to be equal to the parent value (usualy an integer)

请记住,在进行比较时,它必须等于父值(通常是整数)

another course of concerns is postback issues!

另一个关注点是回发问题!

回答by Dee

Its as simple as this...

就这么简单……

ddlstate.SelectedIndex = ddlstate.Items.IndexOf(ddlstate.Items.FindByValue(dt.Rows[0]["state"].ToString()));

stateis column name in database.

state是数据库中的列名。

ddlstateis your dropdownlist id.

ddlstate是您的下拉列表 ID。