ASP.NET List Control
- by Ricardo Peres
Today I developed a simple control for generating lists in ASP.NET, something that the base class library does not contain; it allows for nested lists where the list item types and images can be configured on a list by list basis. Since it was a great fun to develop, I'd like to share it here. Here is the code:
[ParseChildren(true)]
[PersistChildren(false)]
public class List: WebControl
{
public List(): base("ul")
{
this.Items = new List();
this.ListStyleType = ListStyleType.Auto;
this.ListStyleImageUrl = String.Empty;
this.CommonCssClass = String.Empty;
this.ContainerCssClass = String.Empty;
}
[DefaultValue(ListStyleType.Auto)]
public ListStyleType ListStyleType
{
get;
set;
}
[DefaultValue("")]
[UrlProperty("*.png;*.gif;*.jpg")]
public String ListStyleImageUrl
{
get;
set;
}
[DefaultValue("")]
[CssClassProperty]
public String CommonCssClass
{
get;
set;
}
[DefaultValue("")]
[CssClassProperty]
public String ContainerCssClass
{
get;
set;
}
[Browsable(false)]
[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
public List Items
{
private set;
get;
}
protected override void Render(HtmlTextWriter writer)
{
String cssClass = String.Join(" ", new String [] { this.CssClass, this.ContainerCssClass });
if (cssClass.Trim().Length != 0)
{
this.CssClass = cssClass;
}
if (String.IsNullOrEmpty(this.ListStyleImageUrl) == false)
{
this.Style[ HtmlTextWriterStyle.ListStyleImage ] = String.Format("url('{0}')", this.ResolveClientUrl(this.ListStyleImageUrl));
}
if (this.ListStyleType != ListStyleType.Auto)
{
switch (this.ListStyleType)
{
case ListStyleType.Circle:
case ListStyleType.Decimal:
case ListStyleType.Disc:
case ListStyleType.None:
case ListStyleType.Square:
this.Style [ HtmlTextWriterStyle.ListStyleType ] = this.ListStyleType.ToString().ToLower();
break;
case ListStyleType.LowerAlpha:
this.Style [ HtmlTextWriterStyle.ListStyleType ] = "lower-alpha";
break;
case ListStyleType.LowerRoman:
this.Style [ HtmlTextWriterStyle.ListStyleType ] = "lower-roman";
break;
case ListStyleType.UpperAlpha:
this.Style [ HtmlTextWriterStyle.ListStyleType ] = "upper-alpha";
break;
case ListStyleType.UpperRoman:
this.Style [ HtmlTextWriterStyle.ListStyleType ] = "upper-roman";
break;
}
}
base.Render(writer);
}
protected override void RenderChildren(HtmlTextWriter writer)
{
foreach (ListItem item in this.Items)
{
this.writeItem(item, this, 0);
}
base.RenderChildren(writer);
}
private void writeItem(ListItem item, Control control, Int32 depth)
{
HtmlGenericControl li = new HtmlGenericControl("li");
control.Controls.Add(li);
if (String.IsNullOrEmpty(this.CommonCssClass) == false)
{
String cssClass = String.Join(" ", new String [] { this.CommonCssClass, this.CommonCssClass + depth });
li.Attributes [ "class" ] = cssClass;
}
foreach (String key in item.Attributes.Keys)
{
li.Attributes[key] = item.Attributes [ key ];
}
li.InnerText = item.Text;
if (item.ChildItems.Count != 0)
{
HtmlGenericControl ul = new HtmlGenericControl("ul");
li.Controls.Add(ul);
if (String.IsNullOrEmpty(this.ContainerCssClass) == false)
{
ul.Attributes["class"] = this.ContainerCssClass;
}
if ((item.ListStyleType != ListStyleType.Auto) || (String.IsNullOrEmpty(item.ListStyleImageUrl) == false))
{
if (String.IsNullOrEmpty(item.ListStyleImageUrl) == false)
{
ul.Style[HtmlTextWriterStyle.ListStyleImage] = String.Format("url('{0}');", this.ResolveClientUrl(item.ListStyleImageUrl));
}
if (item.ListStyleType != ListStyleType.Auto)
{
switch (this.ListStyleType)
{
case ListStyleType.Circle:
case ListStyleType.Decimal:
case ListStyleType.Disc:
case ListStyleType.None:
case ListStyleType.Square:
ul.Style[ HtmlTextWriterStyle.ListStyleType ] = item.ListStyleType.ToString().ToLower();
break;
case ListStyleType.LowerAlpha:
ul.Style [ HtmlTextWriterStyle.ListStyleType ] = "lower-alpha";
break;
case ListStyleType.LowerRoman:
ul.Style [ HtmlTextWriterStyle.ListStyleType ] = "lower-roman";
break;
case ListStyleType.UpperAlpha:
ul.Style [ HtmlTextWriterStyle.ListStyleType ] = "upper-alpha";
break;
case ListStyleType.UpperRoman:
ul.Style [ HtmlTextWriterStyle.ListStyleType ] = "upper-roman";
break;
}
}
}
foreach (ListItem childItem in item.ChildItems)
{
this.writeItem(childItem, ul, depth + 1);
}
}
}
}
[Serializable]
[ParseChildren(true, "ChildItems")]
public class ListItem: IAttributeAccessor
{
public ListItem()
{
this.ChildItems = new List();
this.Attributes = new Dictionary();
this.Text = String.Empty;
this.Value = String.Empty;
this.ListStyleType = ListStyleType.Auto;
this.ListStyleImageUrl = String.Empty;
}
[DefaultValue(ListStyleType.Auto)]
public ListStyleType ListStyleType
{
get;
set;
}
[DefaultValue("")]
[UrlProperty("*.png;*.gif;*.jpg")]
public String ListStyleImageUrl
{
get;
set;
}
[DefaultValue("")]
public String Text
{
get;
set;
}
[DefaultValue("")]
public String Value
{
get;
set;
}
[Browsable(false)]
public List ChildItems
{
get;
private set;
}
[Browsable(false)]
public Dictionary Attributes
{
get;
private set;
}
String IAttributeAccessor.GetAttribute(String key)
{
return (this.Attributes [ key ]);
}
void IAttributeAccessor.SetAttribute(String key, String value)
{
this.Attributes [ key ] = value;
}
}
[Serializable]
public enum ListStyleType
{
Auto = 0,
Disc,
Circle,
Square,
Decimal,
LowerRoman,
UpperRoman,
LowerAlpha,
UpperAlpha,
None
}
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.0.320/scripts/clipboard.swf';
SyntaxHighlighter.brushes.CSharp.aliases = ['c#', 'c-sharp', 'csharp'];
SyntaxHighlighter.all();