Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何扩展ListItem以便在BulletList中有子项_C#_Asp.net - Fatal编程技术网

C# 如何扩展ListItem以便在BulletList中有子项

C# 如何扩展ListItem以便在BulletList中有子项,c#,asp.net,C#,Asp.net,使用asp.net C#。 我有BulletList,我想添加将与childs一起呈现的ListItem。 也就是说,在每个中,我可以添加更多要渲染的控件 我怎样才能做到这一点 谢谢我不知道BulletedList为什么有一组列表项。ListItems通常用于表单元素(因此是Selected、Value、Text属性),用于项目符号列表没有意义。也没有包含子ListItems的属性可供您实现目标。我建议您使用自己的类来完成此操作。下面是一个快速模型: public static class Bu

使用asp.net C#。 我有BulletList,我想添加将与childs一起呈现的ListItem。 也就是说,在每个
  • 中,我可以添加更多要渲染的控件

    我怎样才能做到这一点


    谢谢

    我不知道BulletedList为什么有一组列表项。ListItems通常用于表单元素(因此是Selected、Value、Text属性),用于项目符号列表没有意义。也没有包含子ListItems的属性可供您实现目标。我建议您使用自己的类来完成此操作。下面是一个快速模型:

    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; }
    }
    
    公共静态类公告列表
    {
    公共静态字符串RenderList(列表){
    var sb=新的StringBuilder();
    if(list!=null&&list.Count>0)
    {
    某人加上(“
      ”); foreach(列表中的变量项){ 某人追加(项目内容); sb.追加(BulletList.RenderList(item.Children)); } 某人追加(“
    ”); } 使某人返回字符串(); } } 公共类BulletListItem { 公共字符串内容{get;set;} 公共列表子项{get;set;} }
    然后您可以创建包含子项的列表并将其输出

    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));
    
    var items=newlist();
    添加(新BulletListItem(){Content=“Root 1”});
    添加(新BulletListItem(){Content=“Root 2”,Children=new List(){new BulletListItem(){Content=“Child 2.1”}}});
    添加(新BulletListItem(){Content=“Root 3”});
    响应.写入(BulletList.RenderList(项目));
    
    是否需要从WebControl继承?不应该执行控件?使用web控件取决于您想要完成的任务。例如,如果您希望维护回发之间的状态,这是web控件的主要用途,那么您可以将其转换为适合该模型的状态。为了简单地输出一个不需要维护状态的列表,只输出html更容易。如果您需要在页面上进行回发,我建议您使用ajax,只返回所需的json/数据,并通过javascript操作页面内容。您也可以使用UpdatePanel,但这取决于您最熟悉的内容。