Colors in the ListBox..

Hi Guys,
I am using Listbox in my project and based on certain conditions the colors in the list is either "Red" and "Grey" . The code works fine when you load the page for the first time. But any point if the page is postback then I loose all my colors from the list box.

Any ideas?

private void getSection()
    {
        DataTable dtSection = new DataTable();
        RMFExtractBus objRMFBus = new RMFExtractBus();
        dtSection = objRMFBus.GetSystemDescp();
        if (dtSection != null)
        {
            if (dtSection.Rows.Count > 0)
            {
                ListItem lstItem = null;
                for (int i = 0; i < dtSection.Rows.Count - 1; i++)
                {
                    if (dtSection.Rows[i]["Sub_System_ID"].ToString() != "")
                    {
                        //Show flag is true and spec flag is true
                        if (dtSection.Rows[i]["System_Show_Flag"].ToString() == "True" && dtSection.Rows[i]["System_Spec_Need_Flag"].ToString() == "True")
                        {
                            lstItem = new ListItem(Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + dtSection.Rows[i]["Sub_System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString() + ";" + dtSection.Rows[i]["Sub_System_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:Red");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                        //Show flag is true and spec flag is false
                        else if (dtSection.Rows[i]["System_Show_Flag"].ToString() == "True" && dtSection.Rows[i]["System_Spec_Need_Flag"].ToString() == "False")
                        {
                            lstItem = new ListItem(Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + dtSection.Rows[i]["Sub_System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString() + ";" + dtSection.Rows[i]["Sub_System_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:Blue");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                        //Show flag is false and spec flag is false
                        else if (dtSection.Rows[i]["System_Show_Flag"].ToString() == "False" && dtSection.Rows[i]["System_Spec_Need_Flag"].ToString() == "False")
                        {
                            lstItem = new ListItem(Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + dtSection.Rows[i]["Sub_System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString() + ";" + dtSection.Rows[i]["Sub_System_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:Green");
                            lstProjSpecs.Items.Add(lstItem);

                        }
                        //Show flag is false and spec flag is true
                        else if (dtSection.Rows[i]["System_Show_Flag"].ToString() == "False" && dtSection.Rows[i]["System_Spec_Need_Flag"].ToString() == "true")
                        {
                            lstItem = new ListItem(Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + dtSection.Rows[i]["Sub_System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString() + ";" + dtSection.Rows[i]["Sub_System_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:Gray");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                        else
                        {
                            lstItem = new ListItem(Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + dtSection.Rows[i]["Sub_System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString() + ";" + dtSection.Rows[i]["Sub_System_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:Black");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                    }
                    else
                    {

                        //Show flag is true and spec flag is true
                        if (dtSection.Rows[i]["System_Show_Flag"].ToString() == "True" && dtSection.Rows[i]["System_Spec_Need_Flag"].ToString() == "True")
                        {
                            lstItem = new ListItem(dtSection.Rows[i]["System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:LightBlue");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                        //Show flag is true and spec flag is false
                        else if (dtSection.Rows[i]["System_Show_Flag"].ToString() == "True" && dtSection.Rows[i]["System_Spec_Need_Flag"].ToString() == "False")
                        {
                            lstItem = new ListItem(dtSection.Rows[i]["System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:LightGray");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                        //Show flag is false and spec flag is false
                        else if (dtSection.Rows[i]["System_Show_Flag"].ToString() == "False" && dtSection.Rows[i]["System_Spec_Need_Flag"].ToString() == "False")
                        {
                            lstItem = new ListItem(dtSection.Rows[i]["System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:LightSkyBlue");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                        //Show flag is false and spec flag is true
                        else if (dtSection.Rows[i]["System_Show_Flag"].ToString() == "False" && dtSection.Rows[i]["System_Spec_Need_Flag"].ToString() == "true")
                        {
                            lstItem = new ListItem(dtSection.Rows[i]["System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:LightSlateGray");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                        else
                        {
                            lstItem = new ListItem(dtSection.Rows[i]["System_Descp"].ToString(), dtSection.Rows[i]["System_Descp_ID"].ToString());
                            lstItem.Attributes.Add("style", "color:Black");
                            lstProjSpecs.Items.Add(lstItem);
                        }
                    }
                }
            }
        }
    }

Now I did call the same after the post back also, but no luck. Any help is appreciated...
rajeshsachdevaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RejojohnyCommented:
where are you clearing the listbox lstProjSpecs before adding the items again after postback?
After postback, can you please see the source of the page whether it still has the style attributes to the listitem?

Rejo
0
rajeshsachdevaAuthor Commented:
Hi Rejo

I am actually performing the clearselection before my for loop starts also set the SelectedIndexChange to -1.

After doing R&D, this is known bug with studio 2005. The problem is not there with the framework 3.5. The actual problem is in the viewstate as it maintaining the attributes values (somehow)?
0
rajeshsachdevaAuthor Commented:
Found the solution.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

RejojohnyCommented:
what was the solution?
0
rajeshsachdevaAuthor Commented:
All I have to do is:

Listbox.items.Clear();
0
RejojohnyCommented:
if that is what you did to solve your problem, don't you think my comment above deserves some credit?

>>where are you clearing the listbox lstProjSpecs before adding the items again after postback?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rajeshsachdevaAuthor Commented:
Thanks for your comment.List clear needs to happen after postback also.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.