加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 使用Linq查询从DropDownList中选择一个ListItem

发布时间:2020-12-16 07:34:37 所属栏目:asp.Net 来源:网络整理
导读:我试图使用 Linq查询在下拉列表控件中查找和设置所选值. Dim qry = From i In ddlOutcome.Items _ Where i.Text.Contains(value) Dim selectedItem As ListItem = qry.First ddlOutcome.SelectedValue = selectedItem.Value 即使文档说DropDownList.Items集
我试图使用 Linq查询在下拉列表控件中查找和设置所选值.

Dim qry = From i In ddlOutcome.Items _
           Where i.Text.Contains(value)


 Dim selectedItem As ListItem = qry.First

 ddlOutcome.SelectedValue = selectedItem.Value

即使文档说DropDownList.Items集合实现IEnumerable,我在Where子句中得到一个错误,Option Strict ON不允许后期绑定!

解决方法

我可以用C#给你一个答案,我希望它可以帮助你.

使用DropDownlist方法最简单的方法,比linq查询更好:

DropDownList1.SelectedIndex = 
       DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("2"));

如果你想要linq查询,它将是这样的:

var selected=from i in DropDownList1.Items.Cast<ListItem>()
                     where ((ListItem)i).Text.Contains("2") select i;

DropDownList1.SelectedValue = selected.ToList()[0].Text;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读