Jim Bedson
Jim Bedson

Reputation: 85

asp.net Listbox Formatting not persisting after postback

I have a ListBox that has highlighted items based on their status. If they are incomplete, they are colored red. After selecting one of the items from the list and displaying it's relate data, the coloring doesn't work. How can I keep this formatting persistent?

Code:

if (!IsPostBack)
        {
            MarkIncompleteList();
            DataTable dt = Data.GetOrderStatusTypes();

            ddlOrderStatusTypes.DataSource = dt;
            ddlOrderStatusTypes.DataTextField = "StatusName";
            ddlOrderStatusTypes.DataValueField = "StatusID";
            ddlOrderStatusTypes.DataBind();




            if (Request.QueryString["OrderID"] != "")
            {
                Data.GetOrderByNumber(ref currOrder, Request.QueryString["OrderID"]);
                DisplayOrderData();
            }
        }
        else

MarkIncompleteList();

protected void MarkIncompleteList()
    {
        // get a list of orders that are not complete for allerting
        List<string> iOrders = Data.GetIncomepletedOrders();

        lbOrderID.Text = "";
        List<string> ol = Data.GetOrdersList();
        // add the order list to the list box change the color if its in the 
        // incomplete list
        foreach (string s in ol)
        {
            if (iOrders.Contains(s))
            {
                lbCurrentOrders.Items.Add(s);
                lbCurrentOrders.Items[lbCurrentOrders.Items.Count - 1].Attributes.Add("style", "color:red");

            }
            else
                lbCurrentOrders.Items.Add(s);
        }
    }

Upvotes: 0

Views: 456

Answers (1)

Jim Bedson
Jim Bedson

Reputation: 85

Sorry for the stupid question and thank you everyone for your quick responses. The issue was related to the items in the list. I was NOT clearing the items after the selection. This caused my list to look normal but re-added the formatted list to the end of the original. I've corrected this and it's now working properly.

protected void MarkIncompleteList()
    {
        // remove the old items so the list is refreshed **************
        lbCurrentOrders.Items.Clear();
        // get a list of orders that are not complete for allerting
        List<string> iOrders = Data.GetIncomepletedOrders();

        lbOrderID.Text = "";
        List<string> ol = Data.GetOrdersList();
        foreach (string s in ol)
        {
            if (iOrders.Contains(s))
            {
                lbCurrentOrders.Items.Add(s);
                lbCurrentOrders.Items[lbCurrentOrders.Items.Count - 1].Attributes.Add("style", "color:red");

            }
            else
                lbCurrentOrders.Items.Add(s);
        }


    }

Upvotes: 1

Related Questions