ASP.NET中具有自定义子元素的自定义元素

ASP.NET中具有自定义子元素的自定义元素,asp.net,custom-controls,Asp.net,Custom Controls,我知道可以在ASP.NET中使用用户控件定义自定义标记。但据我所知,您只能向这些控件添加属性。我希望能够嵌入更复杂的数据,稍微简化一下: <myControls:MyGraph id="myGraph1" runat="server"> <colors> <color>#abcdef</color> <color>#123456</color> </colors> </myC

我知道可以在ASP.NET中使用用户控件定义自定义标记。但据我所知,您只能向这些控件添加属性。我希望能够嵌入更复杂的数据,稍微简化一下:

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

#abcdef
#123456

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

当然有可能。对于您的示例,这些类看起来像:

[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; }
}
[ParseChildren(true)]
类MyGraph:WebControl{
列表_colors=新列表();
[PersistenceMode(PersistenceMode.InnerProperty)]
公共列表颜色{
获取{return\u colors;}
}
}
类颜色{
公共字符串值{get;set;}
}
而实际的加价是:

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

您不能为此类目的使用用户控件。如上所述,使用系统继承控制或网络控制。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace ComponentDemo
{
    [ParseChildren( true )]
    public class MyGraph : System.Web.UI.WebControls.WebControl
    {
        private List<Color> _colors;

        public MyGraph() : base() { ;}

        [PersistenceMode( PersistenceMode.InnerProperty )]
        public List<Color> Colors
        {
            get 
            {
                if ( null == this._colors ) { this._colors = new List<Color>(); }
                return _colors; 
            }
        }
    }

    public class Color
    {
        public Color() : base() { ;}
        public string Value { get; set; }
    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ComponentDemo._Default" %>
<%@ Register Assembly="ComponentDemo" Namespace="ComponentDemo" TagPrefix="my" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <my:MyGraph runat="server">
            <Colors>
                <my:Color Value="value1" />
                <my:Color Value="value2" />
                <my:Color Value="value3" />
                <my:Color Value="value4" />
            </Colors>
        </my:MyGraph>
    </div>
    </form>
</body>
</html>
使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 名称空间组件演示 { [儿童(对)] 公共类MyGraph:System.Web.UI.WebControl.WebControl { 私有列表颜色; 公共MyGraph():base(){;} [PersistenceMode(PersistenceMode.InnerProperty)] 公共列表颜色 { 得到 { 如果(null==this.\u colors){this.\u colors=new List();} 返回颜色; } } } 公共类颜色 { 公共颜色():base(){;} 公共字符串值{get;set;} } }
谢谢您的帮助。。所有关于手工构建服务器控件的行话都很难得到一个直接的答案。事后看来,将内部元素视为属性和其他属性是很有意义的。干杯为什么你会发布相互矛盾的答案?