2009-07-01 54 views

回答

6

这是VB代码,这样做......

myListBox.SelectionMode = Multiple 
For each i as listBoxItem in myListBox.Items 
    if i.Value = WantedValue Then 
     i.Selected = true 
    end if 
Next 
12

这里是一个C#示例


(CSS)

<form id="form1" runat="server"> 
     <asp:ListBox ID="ListBox1" runat="server" > 
      <asp:ListItem Value="Red" /> 
      <asp:ListItem Value="Blue" /> 
      <asp:ListItem Value="Green" /> 
     </asp:ListBox> 
     <asp:Button ID="Button1" 
        runat="server" 
        onclick="Button1_Click" 
        Text="Select Blue and Green" /> 
</form> 

(代码隐藏)

protected void Button1_Click(object sender, EventArgs e) 
{ 
    ListBox1.SelectionMode = ListSelectionMode.Multiple;    
    foreach (ListItem item in ListBox1.Items) 
    { 
      if (item.Value == "Blue" || item.Value == "Green") 
      { 
       item.Selected = true; 
      } 
    } 
} 
11

你将不得不使用列表框

foreach (string selectedValue in SelectedValuesArray) 
        { 
         lstBranch.Items.FindByValue(selectedValue).Selected = true; 
        } 
+1

+1这在我看来是最好的选择,因为它只能通过需要的物品,而不是整个列表框集合迭代。我用我自己的解决方案,谢谢Phu! – 2014-02-20 23:35:18

0

我喜欢在那里bill berlington与他解去的FindByValue方法。我不想迭代我的数组中的每个项目的ListBox.Items。这里是我的解决方案:

foreach (int index in indicesIntArray) 
{ 
    applicationListBox.Items[index].Selected = true; 
} 
1

在C#:

foreach (ListItem item in ListBox1.Items) 
{ 
    item.Attributes.Add("selected", "selected"); 
} 
相关问题