2009-07-01 14 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

यहाँ एक सी # नमूना


(aspx)

<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

आप ListBox

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

+1 इस मेरी राय में सबसे अच्छा विकल्प है क्योंकि यह केवल आवश्यक वस्तुओं, नहीं पूरे लिस्टबॉक्स संग्रह के माध्यम से दोहराता। मैंने इसे अपने स्वयं के समाधान में इस्तेमाल किया, धन्यवाद फु! –

0

मुझे पसंद है जहां bill berlington उसके समाधान के साथ जा रहा है की FindByValue विधि का उपयोग करना होगा। मैं ListBox के माध्यम से पुन: प्रयास नहीं करना चाहता हूं। मेरे सरणी में प्रत्येक आइटम के लिए यह लगता है।

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

सी # में: यहाँ मेरी हल है

foreach (ListItem item in ListBox1.Items) 
{ 
    item.Attributes.Add("selected", "selected"); 
} 
संबंधित मुद्दे