TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

ASP.NET Bug: ListItem Attributes

Attributes added to a ListItem on a DropDownList server control don't get rendered. If you use an HtmlSelectControl, they get rendered, but don't persist via ViewState on postback....

private void FillColorList()

{

    foreach(PropertyInfo property in typeof(Color).GetProperties(BindingFlags.Static |

                                                                 BindingFlags.Public |

     BindingFlags.NonPublic))

    {

        if(property.ReflectedType.ToString() == typeof(Color).ToString())

        {

            Color color = (Color) property.GetValue(null, null);

            string htmlColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);

            ListItem item = new ListItem(color.Name, htmlColor);

            item.Attributes.Add("style", String.Format("background-color: {0}", htmlColor));

            this.dropDownListEventColor.Items.Add(item);

        }

    }

}


Digg!

posted on Tuesday, May 04, 2004 1:33 PM

Feedback

# re: ASP.NET Bug: ListItem Attributes 9/4/2004 8:58 AM Amit

Same behavior for CheckBoxList

# re: ASP.NET Bug: ListItem Attributes 9/7/2004 2:25 AM StuartGunter

I've been playing around with this too! Have you found a fix / workaround for this?

# Bug in ASP.NET 1.1 (ListItem) 9/7/2004 4:28 AM StuartGunter

# re: ASP.NET Bug: ListItem Attributes 9/7/2004 9:29 AM Sean Chase

Best I can tell, you have to set the color even on postback. :-(

# re: ASP.NET Bug: ListItem Attributes 3/3/2005 2:32 PM Viktor Pimanov

this code change the color of item "Yes" if it is selected

public class RBL : RadioButtonList
{
public RBL() : base()
{
}
protected override void Render(HtmlTextWriter writer)
{
if(Items[1].Selected)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
base.Render(htw);
string renderstr = sw.GetStringBuilder().ToString();
int beg = renderstr.IndexOf("Yes");
if(beg > 0)
{
renderstr = renderstr.Substring(0,beg)+"<FONT color=\"blue\">Yes</FONT>"+renderstr.Substring(beg+3);
}
writer.Write(renderstr);
}
else
base.Render(writer);
}
}

# re: ASP.NET Bug: ListItem Attributes 2/17/2006 12:56 PM johnccr

There is no solution for framework 1.x, maybe framework 2.0 fixed the issue. see

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q309338

Regards,
JC

# re: ASP.NET Bug: ListItem Attributes 3/29/2007 9:50 AM Boris

Inherit SystemWeb.UI.ListBox, overwrite RenderContens like this:

protected override void RenderContents(HtmlTextWriter writer)
{
ListItemCollection items = this.Items;
int count = items.Count;
if (count > 0)
{
for (int i = 0; i < count; i++)
{
ListItem item = items[i];
writer.WriteBeginTag("option");
if (item.Selected && this.SelectionMode == ListSelectionMode.Multiple)
{
writer.WriteAttribute("selected", "selected");
}
writer.WriteAttribute("value", item.Value, true);

if (item.Attributes.Count > 0)
{
item.Attributes.Render(writer);
}

writer.Write('>');
HttpUtility.HtmlEncode(item.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
}

# 真郁闷,ListItem的Attributes让我碰到了 4/3/2008 6:23 AM 武眉博

??CheckBoxList?Item??Onclick???javascript?? CheckBoxList