Format some lines in listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mai Phuong
    New Member
    • Sep 2009
    • 25

    Format some lines in listbox

    Hi all,

    I have a page of aspx and a listbox. I want to format font for 3 last lines of my listbox with Bold Style by code. Notice that my lisbox has more than 3 lines, and I don't know exactly text for the first three items of the list until my web application runs.

    Lines of text are added to my listbox by the following way:

    Code:
     foreach (DataRow row in dt.Rows)    
     {          
      string Msg = string.Format("{0} : {1}", row["Id"], row["Name_"]);               
    
       lstHisttory.Items.Add(Msg);         
     
       //lstHistory is my listbox                   
    
     }

    Would you give me any sugestion?

    Thanks for your helping!
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    ListItem's are kind of limited when it comes to adding style to them.
    Have you tried using the ListItem.Attrib utes property to add a CSS class to the particular ListItems?

    Something like:
    Code:
    mySpecialListItem.Attributes.Add("class","specialCssStyleClass")
    -Frinny

    Comment

    • onlyprad
      New Member
      • Nov 2009
      • 4

      #3
      hi,

      After populating your listbox in foreach loop, add the below code,

      define a class "boldListIt em" in CSS then,

      lstHisttory.Ite ms[lstHisttory.Ite ms.Count - 3].Text =
      "<span class=\"boldLis tItem\">" +
      lstHisttory.Ite ms[lstHisttory.Ite ms.Count - 3].Text +
      "</span>"

      lstHisttory.Ite ms[lstHisttory.Ite ms.Count - 2].Text =
      "<span class=\"boldLis tItem\">" +
      lstHisttory.Ite ms[lstHisttory.Ite ms.Count - 2].Text +
      "</span>"

      lstHisttory.Ite ms[lstHisttory.Ite ms.Count - 1].Text =
      "<span class=\"boldLis tItem\">" +
      lstHisttory.Ite ms[lstHisttory.Ite ms.Count - 1].Text +
      "</span>"


      As Frinny said, you can add attribute to listitem but when you postback the style is lost. It happened to me. So if you add the class directly to the listitem text, it is retained even after postback.

      You want to bold only last 3 items, so its ok to do it above way.
      But if you want to bold more then it will be good to do in a more logical way by using a for loop.

      Regards,
      only

      Comment

      • Mai Phuong
        New Member
        • Sep 2009
        • 25

        #4
        @Frinavale: It worked well, but all items' styles in my listbox changed, not just the 3 last lines.

        @onlyprad: After doing as your suggestion, the text of 3 last lines changed in content, not in format: "<span class=\"boldLis tItem\"> my text </span>

        I think there is no way to do this because of its limited attributes. Maybe I will use datagridview.

        Thank you all!

        Comment

        • onlyprad
          New Member
          • Nov 2009
          • 4

          #5
          It works for me.

          the span tag will be rendered as html. It should not appear as it is.

          Regards,
          only

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Mai Phuong they will all take on the same style if you apply the style to all of them. Instead of looping through all of your items, just add the style to the last 3 items.

            Comment

            • Mai Phuong
              New Member
              • Sep 2009
              • 25

              #7
              @Onlyprad: I know your intention, but I can't understand why it does't work ...

              @Frinavale: But how to add the style to the last three items?

              With syntax you mentioned before : lstHisttory.Att ributes.Add("cl ass", "boldListItem") ; it then apply for all of my items .

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Don't apply the style to the whole list.
                Apply it to the individual ListItems...and not all of the ListItems, just the last 3.

                -Frinny

                Comment

                • Mai Phuong
                  New Member
                  • Sep 2009
                  • 25

                  #9
                  haiiiz ... but I can't distinguish between "individual ListItems" and "all of the ListItems". If I have lstHistory as my listbox, could you leave some code lines for me to understand more about "individual ListItems" and "all of the ListItems"?

                  Thank kiu!

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    It would really help if we worked with your code.
                    They way I would do something would be completely different from the way you would do something.

                    Please post the code that you are using to populate the ListBox.

                    -Frinny

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Ok, I'm sorry I'm tired and a little under the weather...you already did post your code:

                      Code:
                       foreach (DataRow row in dt.Rows)    
                       {          
                        string Msg = string.Format("{0} : {1}", row["Id"], row["Name_"]);               
                         lstHisttory.Items.Add(Msg);        
                       }
                      So you are just using a String and you aren't actually creating a new ListItem.
                      Change your code to use a ListItem object instead.
                      When you get to the third last row change the style:

                      Code:
                       for (i=0; i<dt.Rows.Length-1; i++)    //it might be dt.Rows.Count??
                       {          
                        DataRow row = dt.Rows[i];
                        string Msg = string.Format("{0} : {1}", row["Id"], row["Name_"]);               
                        ListItem item = new ListItem(msg);
                        if(i > dt.Rows.Length-4)
                        { item.Attributes.Add("class = 'theClass'"); }
                         lstHisttory.Items.Add(item);        
                       }
                      The above code probably doesn't work properly but it should give you an idea of what to do.

                      Good night,

                      -Frinny

                      Comment

                      • Mai Phuong
                        New Member
                        • Sep 2009
                        • 25

                        #12
                        yep, thank kiu Frinavale so much!

                        In my country it is just 4:00 pm now. Wish you to have a new nice coming day!

                        Comment

                        Working...