.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Coloring DropDown ListBox Items

Posted By:      Posted Date: September 29, 2010    Points: 0   Category :ASP.Net

I have a situation where some dropdown listboxes contain some items, then there's a separator line, and then there are more items.  The ones above the separator line reflect the "commonly used items".  To better highlight these I was using code like this to individually color those specfic items:

         foreach (ListItem item in ddList.Items)
          if (item.Value == "-1")
            unAssigned = true;

          if (!unAssigned)
            item.Attributes.Add("style", "color:#5558ff");

This code works fine EXCEPT that once a PostBack occurs then the highlighting disappears.  The items in the dropdown listbox remain (without repopulation) but not the highlighting.

I'm curious why this is happening and whether there's a way for the highlighting to "stick" short of calling this code from the Page_Load event handler every Postback?


View Complete Post

More Related Resource Links

How to create a dynamically populated items in a listbox and use it as a navigation?

Hello, I am trying to create navigation inside a listbox menu. I load data from xml to populate a list. Each item in the list box should be a link or button. Then, I create a frame to load xaml pages in it. I tested to load pages employing button control, outside listbox with on click event and it worked well. However, I cannot make it work with list box items. I am hoping to get help from you. Any sample or ideas are highly appreciated. Below is the code:  XML: <?xml version="1.0" encoding="utf-8"?> <Pages> <page id="page01"> <name>Page 01</name> </page> <page id="page02"> <name>Page 02</name> </page> </Pages> C# to initiate a click o n listbox item and get the path to page: private void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem); string itemName = lbi.Content.ToString(); if ( Nav_ListBox.SelectedItem.Equals("Page01" ) ) { ContentFrame.Source = new Uri("Pages/Page01.xaml", UriKind.RelativeOrAbsolute); } } XAML:  <ListBox x:Name="Nav_ListBox" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Background="#FFF2F2F2" ItemTemplate="{DynamicReso

Animate listbox items from ItemTemplate

I want to have a slide-in effect for listbox items, when it's first loaded. I'm using ItemTemplate for listbox items and I coule do the slide-in effect from within the template using RenderTransform and DocubleAnimation. but as the Listbox is vertualized, it does the effect everytime new page of data is loaded in listbox. What I want is to animate the visible items in listbox only when DataContext is changed the first time, like messages list in 'Windows Live Mail' thanks in advance.  

Selecting Items in a databound ListBox

Hi All, I'm facing a problem while trying to programatically preselect few items in a databound ListBox. I have set the datasource of the listbox to a DataTable, with required Data and Value member columns. The selection mode of the listbox is MULTIEXTENDED. Platform: .NET  1.1 (not sure if the problem is relevant to other versions) I tried preselecting items in a different ListBox that is not bound to any datasource. I'm able to preselect items using the SetSelected method. Perfect!   How can I achieve the same for a databound ListBox? What am I missing? Any help will be very much appreciated.   Regards, Amirtha

Reverse Superscript for Listbox Items in C# windows

I want to show Reverse Super script formate for Listbox Items when i check CheckBox using c# windows forms. thanks ram

How to get values setted in custom items from a ListBox




I have a ListBox which looks like that :

Its DataTemplat is :


<DataTemplate x:Key="IndexDataTemplate">
   <StackPanel Margin="0,0,0,5">
    <TextBlock Height="Auto"

Problem when adding items to a listbox from different threads


I have an application in which different threads add items to a listbox throughout the application's execution (not at startup). But when one of the threads tries to add an item to the listbox, a System.InvanlidOperationException is thrown and the error message is: "The calling thread cannot access this object because a different thread owns it." (In my scenario the "calling thread" is one of the worker threads that add items to the listbox, and the "different thread" is the UI thread.)

So basically I just need a simple way to allow the threads to share the listbox object, or, failing that, some other workaround.

Adding Items to ListBox with animation



I have the following code:

<DataTemplate x:Key="DataTemplate1">
		<EventTrigger RoutedEvent="FrameworkElement.Loaded" SourceName="textBlock">

WPF ListBox doesn't Group items if I don't use ItemSource.


WPF ListBox doesn't Group items if I don't use ItemSource.

This code snippet works but I use



private ObservableCollection<

Populating a DropDown ListBox from an Enum


I have this simple Enum defined:

    public enum Grade : byte
      Up = 1,
      Down = 2,
      Flat = 4

I've tried using it to populate a DropDown ListBox like this:

    ddList.DataSource = Enum.GetValues(typeof(Constants.Grade));

The problem is that BOTH the Text and the Value of each ListItem ends up with the text string.  What I want, of course, is for each ListItem's Value property to be populated with the numeric value.

Is there a simple way to do this?


Dropdown lists - showing items based on value


Hi, i have over 100 items all loaded in a dropdown list. They all are assigned a value between 1-10 which represent 10 categories.

When certain buttons are clicked, i want the dropdown list to display only items with a value "1" or "2", etc.

How can this be done?

dropdown list items in correspondig colors rather than black


hi can any body help me to code,

i have a dropdownlist for which i have items like red,blue,yellow,green...etc  but these items default static color will be black for all items,

but my requirement is to Red as red color Blue as blue color Green as green color

<asp:DropDownList ID="ddl_node_color" runat="server">
           <asp:ListItem>--Auto Fill Color--</asp:ListItem>

Lookup dropdown list > 20 items renders funny


On a MOSS site with the Publishing module enabled I'm experiencing some weird rendering issues with dropdown lists for Lookup fields in edit mode. When clicking the dropdown to select a value, the dropdown pops up somewhere entirely different on the page. I have googled this thoroughly but haven't really found any good solutions to this problem yet.
I understand that SharePoint changes how a dropdown is rendered when the number of items is more than 20, but I'm still hoping that this issue can be fixed in the masterpage and/or stylesheet?

Any thoughts and/or solutions?


How to unselect selected items in Listbox after SelectedIndexChanged


Hello i have a listbox LbGeneratedExcelLinks  when i click inside of it on an item it download the selected item

here s my code

Protected Sub LbGeneratedExcelLinks_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles LbGeneratedExcelLinks.SelectedIndexChanged
        Dim RedirectUrl As String = LbGeneratedExcelLinks.SelectedValue
        LbGeneratedExcelLinks.SelectedIndex = Nothing
    End Sub

problem is if i want to click on another button or other event it keeps showing the dialog open or save file the one i put inside selectedindexChanged

 i need help to unselect after the selectedindexchanged i think it will solve my problem

Listbox hyperlink items


Hello , i want to create a dynamic listbox where items are links (each item is a hyperlink)

here 's my code

Using sw As StreamWriter = New StreamWriter(Server.MapPath("~/Output/" & weeknumber & ".xls"))
                    Dim hlk As New HyperLink
                    hlk.NavigateUrl = ("~/Output/" & weeknumber & ".xls")

                    Using hw As HtmlTextWriter = New HtmlTextWriter(sw)

Extending a ListBox Control - How to make the extended type require the listbox items implement a ch


I am working on a drag and droppable timeline control, and I am making decent progress.  What I have so far will work decently enough with MVVM design patterns, but I am inexperienced with this kind of advanced control extending so I am sure that some of my methods are crude at best.

I have an extended content presenter control that I want to ensure is what is actually used inside my specialized listbox.  Right now I do the following:

<DataTemplate x:Key="TempTemplate" DataType="{x:Type local:TempDataType}">

			<tt:TimeLineItemControl StartTime="{Binding Path=StartTime}

Move items from one listbox to another listbox in C# windows application.


Hi, I have two listbox controls. I have to move the selected items from first list box to second list box and vice versa. Also i have to move all the items from listbox1 to listbox2 and vice versa. . The listbox box is already assigned the datasource property. The datasource for both list box is dataset. Please help in solving this issue.


Thanks in advance


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend