Style DropDownList's ListItems Individually

D

Doug

I want to display a drop-down list (System.Web.UI.WebControls.DropDownList)
loaded with font names the user can select (Verdana, Garamond, etc)... got
this far no problem.

Now I'd like to have the list show ListItem styled with the font being
listed (ie., the "Verdana" ListItem is styled as Verdana).

Here's my code that doesn't work. Note fontFaces is a string array that
lists the fonts being loaded into the DDL).

for (int i=0; i < (fontFaces.GetUpperBound(0)); i++) {
System.Web.UI.WebControls.ListItem nextItem = new ListItem(fontFaces);
nextItem.Attributes.CssStyle.Add("font-family", fontFaces);
DDL_Fonts.Items.Add(nextItem);
}

What would it take to accomplish the effect I'm after?

Thanks!
 
D

Doug

....didn't work... any other ideas?



Eliyahu Goldin said:
nextItem.Attributes["style"]=String.Format ("font-family:'{0}'",
fontFaces);

Eliyahu

Doug said:
I want to display a drop-down list (System.Web.UI.WebControls.DropDownList)
loaded with font names the user can select (Verdana, Garamond, etc)... got
this far no problem.

Now I'd like to have the list show ListItem styled with the font being
listed (ie., the "Verdana" ListItem is styled as Verdana).

Here's my code that doesn't work. Note fontFaces is a string array that
lists the fonts being loaded into the DDL).

for (int i=0; i < (fontFaces.GetUpperBound(0)); i++) {
System.Web.UI.WebControls.ListItem nextItem = new ListItem(fontFaces);
nextItem.Attributes.CssStyle.Add("font-family", fontFaces);
DDL_Fonts.Items.Add(nextItem);
}

What would it take to accomplish the effect I'm after?

Thanks!

 
D

Doug

still no dice... The DDL gets populated with the list of fonts as expected -
just not styled.

Here is all my code - exactly as I have it - including your most recent
suggestion.

IN CODE-BEHIND (executes in Page_Load if not PostBack):

string[] fontFaces = dataClass.GetFontsList();
for (int i=0; i < (fontFaces.GetUpperBound(0) + 1); i++) {
System.Web.UI.WebControls.ListItem nextItem = new ListItem(fontFaces);
nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);
DDL_txtFont.Items.Add(nextItem);
}


IN HTML:
<asp:DropDownList CssClass="ddlText1" id="DDL_txtFont" AutoPostBack="false"
EnableViewState="true" Width="150px" runat="server"></asp:DropDownList>




Eliyahu Goldin said:
try with no strokes:

nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);

should work.

Eliyahu

Doug said:
...didn't work... any other ideas?



Eliyahu Goldin said:
nextItem.Attributes["style"]=String.Format ("font-family:'{0}'",
fontFaces);

Eliyahu

I want to display a drop-down list
(System.Web.UI.WebControls.DropDownList)
loaded with font names the user can select (Verdana, Garamond,
etc)...
got
this far no problem.

Now I'd like to have the list show ListItem styled with the font being
listed (ie., the "Verdana" ListItem is styled as Verdana).

Here's my code that doesn't work. Note fontFaces is a string array that
lists the fonts being loaded into the DDL).

for (int i=0; i < (fontFaces.GetUpperBound(0)); i++) {
System.Web.UI.WebControls.ListItem nextItem = new ListItem(fontFaces);
nextItem.Attributes.CssStyle.Add("font-family", fontFaces);
DDL_Fonts.Items.Add(nextItem);
}

What would it take to accomplish the effect I'm after?

Thanks!


 
E

Eliyahu Goldin

nextItem.Attributes["style"]=String.Format ("font-family:'{0}'",
fontFaces);

Eliyahu
 
D

Doug

Please note that in the HTML in my last post the following attribute was
included in the DDL declaration: CssClass="ddlText1"
I removed that - and the problem still persists (i.e., no styling as
expected from the code-behind).



Doug said:
still no dice... The DDL gets populated with the list of fonts as expected -
just not styled.

Here is all my code - exactly as I have it - including your most recent
suggestion.

IN CODE-BEHIND (executes in Page_Load if not PostBack):

string[] fontFaces = dataClass.GetFontsList();
for (int i=0; i < (fontFaces.GetUpperBound(0) + 1); i++) {
System.Web.UI.WebControls.ListItem nextItem = new ListItem(fontFaces);
nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);
DDL_txtFont.Items.Add(nextItem);
}


IN HTML:
<asp:DropDownList CssClass="ddlText1" id="DDL_txtFont" AutoPostBack="false"
EnableViewState="true" Width="150px" runat="server"></asp:DropDownList>




Eliyahu Goldin said:
try with no strokes:

nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);

should work.

Eliyahu

Doug said:
...didn't work... any other ideas?



nextItem.Attributes["style"]=String.Format ("font-family:'{0}'",
fontFaces);

Eliyahu

I want to display a drop-down list
(System.Web.UI.WebControls.DropDownList)
loaded with font names the user can select (Verdana, Garamond, etc)...
got
this far no problem.

Now I'd like to have the list show ListItem styled with the font being
listed (ie., the "Verdana" ListItem is styled as Verdana).

Here's my code that doesn't work. Note fontFaces is a string array that
lists the fonts being loaded into the DDL).

for (int i=0; i < (fontFaces.GetUpperBound(0)); i++) {
System.Web.UI.WebControls.ListItem nextItem = new
ListItem(fontFaces);
nextItem.Attributes.CssStyle.Add("font-family", fontFaces);
DDL_Fonts.Items.Add(nextItem);
}

What would it take to accomplish the effect I'm after?

Thanks!


 
E

Eliyahu Goldin

try with no strokes:

nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);

should work.

Eliyahu

Doug said:
...didn't work... any other ideas?



Eliyahu Goldin said:
nextItem.Attributes["style"]=String.Format ("font-family:'{0}'",
fontFaces);

Eliyahu

Doug said:
I want to display a drop-down list (System.Web.UI.WebControls.DropDownList)
loaded with font names the user can select (Verdana, Garamond, etc)... got
this far no problem.

Now I'd like to have the list show ListItem styled with the font being
listed (ie., the "Verdana" ListItem is styled as Verdana).

Here's my code that doesn't work. Note fontFaces is a string array that
lists the fonts being loaded into the DDL).

for (int i=0; i < (fontFaces.GetUpperBound(0)); i++) {
System.Web.UI.WebControls.ListItem nextItem = new ListItem(fontFaces);
nextItem.Attributes.CssStyle.Add("font-family", fontFaces);
DDL_Fonts.Items.Add(nextItem);
}

What would it take to accomplish the effect I'm after?

Thanks!


 
E

Eliyahu Goldin

Bad news. DDL emits html <select> with an <option> for every ListItem. And
<option> doesn't support font settings in style.

You can tricking it by using stylesheets with class= unstead of style=. I
doubt it will help though.

Eliyahu

Doug said:
still no dice... The DDL gets populated with the list of fonts as expected -
just not styled.

Here is all my code - exactly as I have it - including your most recent
suggestion.

IN CODE-BEHIND (executes in Page_Load if not PostBack):

string[] fontFaces = dataClass.GetFontsList();
for (int i=0; i < (fontFaces.GetUpperBound(0) + 1); i++) {
System.Web.UI.WebControls.ListItem nextItem = new ListItem(fontFaces);
nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);
DDL_txtFont.Items.Add(nextItem);
}


IN HTML:
<asp:DropDownList CssClass="ddlText1" id="DDL_txtFont" AutoPostBack="false"
EnableViewState="true" Width="150px" runat="server"></asp:DropDownList>




Eliyahu Goldin said:
try with no strokes:

nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);

should work.

Eliyahu

Doug said:
...didn't work... any other ideas?



nextItem.Attributes["style"]=String.Format ("font-family:'{0}'",
fontFaces);

Eliyahu

I want to display a drop-down list
(System.Web.UI.WebControls.DropDownList)
loaded with font names the user can select (Verdana, Garamond, etc)...
got
this far no problem.

Now I'd like to have the list show ListItem styled with the font being
listed (ie., the "Verdana" ListItem is styled as Verdana).

Here's my code that doesn't work. Note fontFaces is a string array that
lists the fonts being loaded into the DDL).

for (int i=0; i < (fontFaces.GetUpperBound(0)); i++) {
System.Web.UI.WebControls.ListItem nextItem = new
ListItem(fontFaces);
nextItem.Attributes.CssStyle.Add("font-family", fontFaces);
DDL_Fonts.Items.Add(nextItem);
}

What would it take to accomplish the effect I'm after?

Thanks!


 
S

Scott G.

You can do this kind of thing using Javascript; check out http://webfx.eae.net/dhtml/select/jsGenerated.html (for IE)

If you are using a gecko browser you can probably get the font-size directly using CSS.

Scott
Please note that in the HTML in my last post the following attribute was
included in the DDL declaration: CssClass="ddlText1"
I removed that - and the problem still persists (i.e., no styling as
expected from the code-behind).



Doug said:
still no dice... The DDL gets populated with the list of fonts as expected -
just not styled.

Here is all my code - exactly as I have it - including your most recent
suggestion.

IN CODE-BEHIND (executes in Page_Load if not PostBack):

string[] fontFaces = dataClass.GetFontsList();
for (int i=0; i < (fontFaces.GetUpperBound(0) + 1); i++) {
System.Web.UI.WebControls.ListItem nextItem = new ListItem(fontFaces);
nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);
DDL_txtFont.Items.Add(nextItem);
}


IN HTML:
<asp:DropDownList CssClass="ddlText1" id="DDL_txtFont" AutoPostBack="false"
EnableViewState="true" Width="150px" runat="server"></asp:DropDownList>




Eliyahu Goldin said:
try with no strokes:

nextItem.Attributes["style"]=String.Format ("font-family:{0}",
fontFaces);

should work.

Eliyahu

Doug said:
...didn't work... any other ideas?



nextItem.Attributes["style"]=String.Format ("font-family:'{0}'",
fontFaces);

Eliyahu

I want to display a drop-down list
(System.Web.UI.WebControls.DropDownList)
loaded with font names the user can select (Verdana, Garamond, etc)...
got
this far no problem.

Now I'd like to have the list show ListItem styled with the font being
listed (ie., the "Verdana" ListItem is styled as Verdana).

Here's my code that doesn't work. Note fontFaces is a string array that
lists the fonts being loaded into the DDL).

for (int i=0; i < (fontFaces.GetUpperBound(0)); i++) {
System.Web.UI.WebControls.ListItem nextItem = new
ListItem(fontFaces);
nextItem.Attributes.CssStyle.Add("font-family", fontFaces);
DDL_Fonts.Items.Add(nextItem);
}

What would it take to accomplish the effect I'm after?

Thanks!


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top