在vb.net的下拉列表中查找值是否包含一个字符串 [英] Find if value contains a string in a drop down list in vb.net

查看:628
本文介绍了在vb.net的下拉列表中查找值是否包含一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,如下所示:

I have a drop down list that looks like this:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                        <asp:ListItem Text="Text1" Value="6,08/04/2015,P"></asp:ListItem>
                        <asp:ListItem Text="Text2" Value="5,11/17/2014,S"></asp:ListItem>
                        <asp:ListItem Text="Text3" Value="4,05/26/2014,P"></asp:ListItem>
                        <asp:ListItem Text="Text4" Value="3,01/20/2014,A"></asp:ListItem>
                        <asp:ListItem Text="Text5" Value="2,10/31/2013,G"></asp:ListItem>
                        <asp:ListItem Text="Text6" Value="1,04/09/2013,P"></asp:ListItem>
</asp:DropDownList>

我需要能够从数据库获取日期,并尝试自动选择正确的日期下拉列表。

I need to be able to get a date from a database and try to automatically select the correct date from the drop down list.

我的代码如下:

dim strDate as string = "10/31/2013"

DropDownList1.selectedvalue.contains(strDate)

类似的东西,但它没有从下拉列表中选择正确的值。

something like that but it does not select the correct value from the drop down.

推荐答案

这两个都应该有效。我将循环遍历下拉列表项集合,然后将该值分割成数组。验证值实际上包含3个值,并获得第二个值与strDate进行比较。一旦您验证了值匹配,设置由以下方法选择的项目,并退出For Each循环,则将重复它将获取第一个。

Either of these should work. I would loop through the dropdown items collection and then split the value into an array. Validate that the values actually contain 3 values and get the second one to compare against strDate. Once you validate the value matches set the item as selected by either of the below methods and exit the For Each loop incase there are duplicates it would grab the 1st one.

Dim strDate As String = "10/31/2013"

For Each item As ListItem In DropDownList1.Items

    Dim split As Array = item.Value.ToString.Split(",")

    'verify the value is split to 3 values
    If split.GetUpperBound(0) = 2 Then

        'get 2nd item which is 1 as array are 0 based
        If split(1) = strDate Then
            item.Attributes.Add("selected", "true")
            Exit For
        End If

    End If

Next

'OR

For Each item As ListItem In DropDownList1.Items

    Dim split As Array = item.Value.ToString.Split(",")

    'verify the value is split to 3 values
    If split.GetUpperBound(0) = 2 Then

        'get 2nd item which is 1 as array are 0 based
        If split(1) = strDate Then
            DropDownList1.SelectedValue = item.Value
            Exit For
        End If

    End If

Next

这篇关于在vb.net的下拉列表中查找值是否包含一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆