ColorfulListBox#region ColorfulListBox
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
namespace CustomizeControls
{
/**//// <summary>
/// Summary description for ColorfulListBox.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:ColorfulListBox runat="server"></{0}:ColorfulListBox>")]
public class ColorfulListBox : System.Web.UI.WebControls.ListBox
{
private ItemColorCollection m_ItemsColor;
public ColorfulListBox()
{
m_ItemsColor = new ItemColorCollection();
}
public ItemColorCollection ItemsColor
{
get
{
m_ItemsColor.Count = this.Items.Count;
return m_ItemsColor;
}
}
/**//// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
this.RenderBeginTag(output);
StringBuilder strbStyle = new StringBuilder();
for( int i=0 ; i < this.Items.Count ; ++i )
{
ListItem li = this.Items[i];
output.Write("<option value="" + li.Value + """);
if ( this.SelectedIndex == i )
{
output.Write(" selected="selected"");
}
/**//*foreach(
string strKey in li.Attributes.CssStyle.Keys )
{
strbStyle.Append(String.Format("{0}:{1};", strKey,
li.Attributes.CssStyle[strKey]));
}
if ( strbStyle.Length > 0 )
{
strbStyle.Insert(0, " style="");
strbStyle.Append(""");
output.Write(strbStyle.ToString());
strbStyle.Remove(0, strbStyle.Length);
}*/
if ( m_ItemsColor[i] != null )
{
Color cForeColor = m_ItemsColor[i].ForeColor;
if ( cForeColor != Color.Empty )
{
strbStyle.Append(String.Format("color:{0};",
cForeColor.Name));
}
Color cBackColor = m_ItemsColor[i].BackColor;
if ( cBackColor != Color.Empty )
{
strbStyle.Append(String.Format("background-color:{0}",
cBackColor.Name));
}
if ( strbStyle.Length > 0 )
{
strbStyle.Insert(0, " style="");
strbStyle.Append(""");
output.Write(strbStyle.ToString());
strbStyle.Remove(0, strbStyle.Length);
}
}
output.Write(">" + li.Text + "</option> ");
}
this.RenderEndTag(output);
}
}
}
#endregion