User177399542 posted
Hi
Based on your requirements I have created a sample for you please check:
<asp:ListBox runat="server" ID="lstBxCompany" SelectionMode="Multiple" Width="500" Height="200">
<asp:ListItem Text="comp1" Value="comp1" />
<asp:ListItem Text="Demo1" Value="Demo1" />
<asp:ListItem Text="comp2" Value="comp2" />
<asp:ListItem Text="comp3" Value="comp3" />
<asp:ListItem Text="Demo2" Value="Demo2" />
<asp:ListItem Text="comp4" Value="comp4" />
<asp:ListItem Text="Demo3" Value="Demo3" />
</asp:ListBox>
<asp:Button Text="Check Values" ID="btnCheckValues" OnClick="btnCheckValues_Click" runat="server" />
Code Behind:
protected void btnCheckValues_Click(object sender, EventArgs e)
{
StringBuilder company = new StringBuilder("comp1, comp2, comp3, comp4");
//--- Here we will split string values and store in array.
string[] comp = company.ToString().Split(',');
//---- Loop through each item stored in array.
foreach (var c in comp)
{
//--- Loop through each item stored in Listbox
foreach (ListItem li in lstBxCompany.Items)
{
if (li.Text == c.Trim())//--- Here Trim() method is used to remove unwanted blank space.
{
li.Selected = true;
}
}
}
}