• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

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...
0
rajeshsachdeva
Asked:
rajeshsachdeva
  • 4
  • 3
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
rajeshsachdevaAuthor Commented:
Thanks for your comment.List clear needs to happen after postback also.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now