Click here to Skip to main content
15,437,002 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hello ,
my listbox selected item are astored in database as concatened with , now i want update my form i want to get selected with highligthing can u guide me or send any snipets
Updated 23-Apr-12 18:29pm

Use event DataBound Event[^]

OnDataBound event, check if the value was earlier selected - if so, apply different style to that listitem. Define a Databound handler for the ListBox. During databind, the execution will go through this method then.

Something like:
protected void lstMultipleValues_DataBound(object sender, EventArgs e)
  foreach (ListItem item in lstMultipleValues.Items)
    //check anything out here
    if (item.Text.StartsWith("B"))
      item.Attributes.Add("style", "color:red");
Share this answer
VJ Reddy 24-Apr-12 1:31am    
Good answer. 5!
Sandeep Mewara 24-Apr-12 2:08am    
If you're trying to manually do it, and are not databound, something like this:

<asp:listbox id="list1" runat="server" selectionmode="Multiple" xmlns:asp="#unknown">
    <asp:listitem value="1" text="Apple"></asp:listitem>
    <asp:listitem value="2" text="Orange"></asp:listitem>
    <asp:listitem value="3" text="Banana"></asp:listitem>
    <asp:listitem value="4" text="Pear"></asp:listitem>


// Selections coming from your database
string selections = "2,4";  

// Create array of selected values
string[] selectedValues = selections.Split(',');

// Make sure there can be multiple value selected on you list box
list1.SelectionMode = ListSelectionMode.Multiple;

foreach (string value in selectedValues)
    ListItem item = list1.Items.FindByValue(value);
    if (item != null)
	item.Selected = true;
Share this answer
VJ Reddy 24-Apr-12 1:32am    
Good answer. 5!
Steve Echols 24-Apr-12 1:36am    
Thanks! (Now I have to go delete that from the page I just mangled :-))

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