A fix for attributes on DDL ListItems
The built in DropDownList doesn't actually render any attributes you set for list items. This stops you from being able to set background colors and such, which people seem to like to do. Well, here's some code that will fix that.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MetaBuilders.WebControls {
public class DDL : System.Web.UI.WebControls.DropDownList {
protected override void RenderContents(HtmlTextWriter writer) {
Boolean selectedItemRendered = false;
for (Int32 i = 0; (i < Items.Count); i++ ) {
ListItem litem = Items[i];
writer.WriteBeginTag("option");
if (litem.Selected) {
if (selectedItemRendered) {
throw new HttpException("DropDownList does not support multiple selection.");
}
selectedItemRendered = true;
writer.WriteAttribute("selected", "selected", false);
}
writer.WriteAttribute("value", litem.Value, true);
litem.Attributes.Render(writer);
writer.Write(">");
HttpUtility.HtmlEncode(litem.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
}
}