Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I have small query,here i am trying to selected listbox items based on String values.

here i bind the Listbox,and i have string ss="Agrinational Insurance Company Inc;American Agri-Business Insurance Company;American Agricultural Insurance Company"
the string items i need to be selected,that items available in Listbox.When user click the button the items will be selected.

any body can u please share the any solution to me.

What I have tried:

I tried to some solutions from google
Posted
Updated 7-Aug-16 22:50pm

1 solution

try like this

C#
protected void Page_Load(object sender, EventArgs e)
      {
          List<string> lst = new List<string>();
          lst.Add("one");
          lst.Add("Agrinational Insurance Company Inc");
          lst.Add("two");
          lst.Add("American Agri-Business Insurance Company");
          lst.Add("three");
          lst.Add("American Agricultural Insurance Company");
          lst.Add("four");
          ListBox1.SelectionMode = ListSelectionMode.Multiple;
          ListBox1.DataSource = lst;
          ListBox1.DataBind();

      }

      protected void btn_Click(object sender, EventArgs e)
      {
          string ss = "Agrinational Insurance Company Inc;American Agri-Business Insurance Company;American Agricultural Insurance Company";
          string[] items = ss.Split(';');
          foreach (ListItem item in ListBox1.Items)
          {
              if (items.Contains(item.Text))
                  item.Selected = true;
          }
      }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900