ASP.NET中的自定义元素自定义子元素 [英] Custom elements in ASP.NET with custom child-elements

查看:178
本文介绍了ASP.NET中的自定义元素自定义子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是可能的ASP.NET定义自定义标签和用户控件。但据我所知,你只能将属性添加到这些控件。我想是能够嵌入更复杂的数据,有点精简版这样的:

I know that it is possible to define custom tags in ASP.NET with User Controls. But as far as I know you can only add attributes to these controls. I would like to be able to embed more complex data, a bit lite this:

<myControls:MyGraph id="myGraph1" runat="server">
   <colors>
     <color>#abcdef</color>
     <color>#123456</color>
   </colors>
</myControls:MyGraph>

这本有可能在ASP.NET?我应该尝试扩展一个ListView?或者,它有一个更好,更正确的解决方案?

It this possible in ASP.NET? Should I try to extend a ListView? Or it there a better and more correct solution?

推荐答案

这当然是可能的。对于示例中的类将如下所示:

It is certainly possible. For your example the classes would look like:

[ParseChildren(true)]
class MyGraph : WebControl {
    List<Color> _colors = new List<Color>();
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public List<Color> Colors {
        get { return _colors; }
    }
}

class Color {
    public string Value { get; set; }
}

和实际的标记是:

<myControls:MyGraph id="myGraph1" runat="server">
   <Colors>
     <myControls:Color Value="#abcdef" />
     <myControls:Color Value="#123456" />
   </Colors>
</myControls:MyGraph>

这篇关于ASP.NET中的自定义元素自定义子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆