Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - listbox item font

 
Thread Tools Search this Thread
Old 07-18-2005, 04:35 PM   #1
Default listbox item font


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


=?Utf-8?B?bWtpZ2Vy?=
  Reply With Quote
Old 07-18-2005, 04:53 PM   #2
=?Utf-8?B?SmFzb24gTCBMaW5k?=
 
Posts: n/a
Default RE: listbox item font
I do not believe that is possible in HTML. You might want to look into using
an ASP.NET DataList.

Jason Lind

"mkiger" wrote:

> 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



=?Utf-8?B?SmFzb24gTCBMaW5k?=
  Reply With Quote
Old 07-18-2005, 09:37 PM   #3
=?Utf-8?B?SmFjZWsgU3Rhd2lja2k=?=
 
Posts: n/a
Default RE: listbox item font
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();
}
}
}


--
C# Dev


"Jason L Lind" wrote:

> I do not believe that is possible in HTML. You might want to look into using
> an ASP.NET DataList.
>
> Jason Lind
>
> "mkiger" wrote:
>
> > 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



=?Utf-8?B?SmFjZWsgU3Rhd2lja2k=?=
  Reply With Quote
Old 07-19-2005, 06:37 AM   #4
russ
 
Posts: n/a
Default Re: listbox item font
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];
}
}
}
}



russ
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox coding problem Ethan James General Help Related Topics 1 06-22-2009 03:12 AM
Special font I need Alexander.Klai General Help Related Topics 0 01-29-2008 11:08 AM
how to pass selected item from listbox of one form to text box of other form chintan Software 0 12-19-2007 11:38 AM
LPG DVD-recorder 'failed to divide' item Ken DVD Video 0 11-13-2007 12:32 AM
Problem of displaying Chinese & Thai font in Java J2ME games on Samsung D520 slchslch General Help Related Topics 0 09-08-2006 11:42 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46