Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

listbox item font

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mkiger - 18 Jul 2005 16:35 GMT
Hey Guys.
 I'm making an asp.net app and I have a question regarding listboxes. I
have a list box and I would like some of the items to be bold based on
certian criteria. Is this possible?
matt
Jason L Lind - 18 Jul 2005 16:53 GMT
I do not believe that is possible in HTML. You might want to look into using
an ASP.NET DataList.

Jason Lind

> Hey Guys.
>   I'm making an asp.net app and I have a question regarding listboxes. I
> have a list box and I would like some of the items to be bold based on
> certian criteria. Is this possible?
> matt
Jacek Stawicki - 18 Jul 2005 21:37 GMT
No way! Standard ListBox is really "simple". Try something like this:

    /// <summary>
    /// ListBoxWithColor
    ///
    ///        Simple control that renders the color attribute of each ListBox option.
    ///       
    ///    Usage:   
    ///        ListBoxWithColor1.Items.Clear();
    ///        ListBoxWithColor1.Items.Add(new ListItem("Hubba","0"));
    ///        ListBoxWithColor1.Items[0].Attributes["style"] =
"background-color:red";  
    ///       
    ///        ListBoxWithColor1.Items.Add(new ListItem("Hubba1","1"));
    ///        ListBoxWithColor1.Items[1].Attributes["style"] =
"background-color:white";  
    ///       
    ///        ListBoxWithColor1.Items[1].Selected = true;
    ///       
    ///    Test with:
    ///        M$ Internet Explorer 6.
    ///       
    /// </summary>
    [ToolboxData("<{0}:ListBoxWithColor runat=server></{0}:ListBoxWithColor>")]
    public class ListBoxWithColor : System.Web.UI.WebControls.ListBox
    {
        public ListBoxWithColor()
        {
            // constructor
        }
        /// <summary>
        /// Override to save color attrib to viewstate
        /// </summary>
        protected override object SaveViewState()
        {
            // creat object array for Item count + 1
            object[] allStates = new object[this.Items.Count + 1];

            // the +1 is to hold the base info like dis
            object baseState = base.SaveViewState();
            allStates[0] = baseState;

            Int32 i = 1;
            // now loop thru and save each Style attrib for the List
            foreach(ListItem li in this.Items)
            {
                allStates[i++] = li.Attributes["style"].ToString();
            }
            return allStates;
        }
        /// <summary>
        /// Override to restore color attrib
        /// </summary>
        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                object[] myState = (object[])savedState;

                // restore base first
                if (myState[0] != null)
                    base.LoadViewState(myState[0]);

                Int32 i = 1;
                foreach(ListItem li in this.Items)
                {
                    // loop thru and restore each style attrib
                    li.Attributes["style"] = (string)myState[i++];
                }
            }
        }
        /// <summary>
        /// Override to render contents of ListBox
        /// </summary>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            foreach(ListItem li in this.Items)
            {
                writer.WriteBeginTag("option");

                if(li.Selected)
                    writer.WriteAttribute("selected","selected",false);

                if(li.Attributes.Count > 0)
                    li.Attributes.Render(writer);  
               
                writer.WriteAttribute("value",li.Value.ToString());  
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Write(li.Text);

                writer.WriteEndTag("option");
                writer.WriteLine();
            }
        }
    }

Signature

C# Dev

> I do not believe that is possible in HTML. You might want to look into using
> an ASP.NET DataList.
[quoted text clipped - 6 lines]
> > certian criteria. Is this possible?
> > matt
russ - 19 Jul 2005 06:37 GMT
works great.  (nearly) exactly what I wanted... :)

changed the save/load viewstate to deal with all the attributes not
just style.

protected override object SaveViewState()
{
// creat object array for Item count + 1
object[] allStates = new object[this.Items.Count + 1];

// the +1 is to hold the base info like dis
object baseState = base.SaveViewState();
allStates[0] = baseState;

Int32 i = 1;
// now loop thru and save each Style attrib for the List
foreach(ListItem li in this.Items)
{
 Int32 j = 0;
 string[][] attributes = new string[li.Attributes.Count][];
 foreach (string attribute in li.Attributes.Keys)
 {
  attributes[j++] = new string[] {attribute,
li.Attributes[attribute]};
 }
 allStates[i++] = attributes;
}
return allStates;
}

protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
 object[] myState = (object[])savedState;

 // restore base first
 if (myState[0] != null)
  base.LoadViewState(myState[0]);

 Int32 i = 1;
 foreach(ListItem li in this.Items)
 {
  // loop thru and restore each style attrib
  //li.Attributes["style"] =
  foreach (string[] attribute in (string[][])myState[i++])
  {
   li.Attributes[attribute[0]] = attribute[1];
  }
 }
}
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.