Maintain Listbox attributes on postback

Posted on 2009-04-17
Last Modified: 2012-05-06
I have duel listboxes. One for Unselected and  One for Selected. I color the individual items based on a value from the dataset. The problem is that the listbox doesn't maintain its attributes on postback. :( So when a button is clicked to move an item from one box to the other the color attribute is lost.  Is their a way to save listitem's color attribute ? Even if I have to readd them then do a check to color the second listbox. something like :
For i = ListView1.Items.Count - 1 To 0 Step -1
            If ListView1.Items(i).ForeColor = Color.Red Then   'moving red only
            End If

Haha..Any help would be appreciated.:D
'Here's how I set the color originally:

If ds.Tables(0).Rows.Count >0 then

   with ListBox1

     .DataSource = ds

     .DataTextField = "name"

     .DataValueField = "someValue"


     Dim i as Interger

    For i = 0 to ds.Tables(0).Rows.Count -1

       If da.Tables(0).Rows(i)("anotherValue") = True Then

         .Items(i).Attributes.Add("style", "color:blue")


          .Items(i).Attributes.Add("style", "color:red")

      end if


   end with

end if

Open in new window

Question by:MiddletownRI
    LVL 12

    Expert Comment

    Unfortunetly ListBox items will not retain custom attributes.  The easiest thing i can thing of is build an array of the attribute values and store it in viewstate.  The index of the attibute would correspond to the index of the item in the listbox.
    LVL 6

    Assisted Solution

    wht1986 is right, as I, too, have recently banged my head against this limitation. Depending on your situation, you might find it easiest to just rebind on every postback.
    LVL 12

    Expert Comment

    did that solve your problem middletownRI?

    Accepted Solution

    These didn't really solve my problem. wht1986 was close in theory.
    I eventually had to create a custom user control that derives from the listbox.
    It saves all the attributes and recreates them on init.
    Still have some issues though. So I'd say, no not solved yet.
    Is there no good solution to this issue with listbox attributes and holding value on postback?
    LVL 12

    Assisted Solution

    Nope nothing out of the box that i know of.  Either a custom control where you save to the control state or add to your page (UserControl) something like:

    public List<string> MyListBoxAttributes
        get {return ViewState("MyListBoxAttributes");}
        set {ViewState("MyListBoxAttributes") = value;

    then when you need attribute of item #3 its just MyListBoxAttributes[3]

    You would set the List whenever you created the listbox items or on the databound event


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now