I' trying to use a Linq query to find and set the selected value in a drop down list control.
Dim qry = From i In ddlOutcome.Items _
Where i.Text.Contains(value)
Dim selectedItem As ListItem = qry.First
ddlOutcome.SelectedValue = selectedItem.Value
Even though the documentation says that the DropDownList.Items collection implements IEnumerable I get an error in the Where clause that Option Strict ON disallows late binding!
I can give you an answer in C#, and i hope it helps you.
The easiest way it to use the methods of DropDownlist, better than linq query:
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("2"));
If you want the linq query it would be like this:
var selected=from i in DropDownList1.Items.Cast<ListItem>()
where ((ListItem)i).Text.Contains("2") select i;
DropDownList1.SelectedValue = selected.ToList()[0].Text;
Anyone thought about:
foreach (ListItem li in drp.Items.Cast<ListItem>().Where(li => li.Value == ""))
{
li.Selected = true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With