How to extend ListItem to be able to have childs inside BulletList
Using asp.net C#.
I have BulletList, and I would like to add ListItem that will render with childs.
That 开发者_如何学JAVAis, inside every <li>
I can add more controls to be rendered.
How can I achieve that?
thanks
I'm not sure why BulletedList has a collection of ListItems. ListItems are generally used for form elements (thus the Selected, Value, Text properties) and doesn't make sense to be used for bulleted lists. There is also no property which contains child ListItems for you to accomplish your goal. I would suggest using your own classes to do this. Here is a quick mockup:
public static class BulletList
{
public static string RenderList(List<BulletListItem> list) {
var sb = new StringBuilder();
if (list != null && list.Count > 0)
{
sb.Append("<ul>");
foreach(var item in list) {
sb.Append(item.Content);
sb.Append(BulletList.RenderList(item.Children));
}
sb.Append("</ul>");
}
return sb.ToString();
}
}
public class BulletListItem
{
public string Content { get; set; }
public List<BulletListItem> Children { get; set; }
}
Then you can create your list with children and output it...
var items = new List<BulletListItem>();
items.Add(new BulletListItem() { Content = "Root 1" });
items.Add(new BulletListItem() { Content = "Root 2", Children = new List<BulletListItem>() { new BulletListItem() { Content = "Child 2.1" }} });
items.Add(new BulletListItem() { Content = "Root 3" });
Response.Write(BulletList.RenderList(items));
精彩评论