【问题标题】:Custom elements in ASP.NET with custom child-elements带有自定义子元素的 ASP.NET 中的自定义元素
【发布时间】:2009-03-23 13:22:55
【问题描述】:

我知道可以使用用户控件在 ASP.NET 中定义自定义标签。但据我所知,您只能为这些控件添加属性。我希望能够嵌入更复杂的数据,这有点精简:

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

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

【问题讨论】:

    标签: asp.net custom-controls


    【解决方案1】:

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

    [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>
    

    【讨论】:

    • 谢谢你。。所有关于手工构建的服务器控件的行话都很难得到一个直接的答案。事后看来,将内部元素视为属性而不是其他元素是很有意义的。干杯!
    【解决方案2】:
    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>
    

    【讨论】:

      【解决方案3】:

      您不能出于此类目的使用 UserControl。如上所述,继承 Control 或 WebControl。

      【讨论】:

      • 为什么发布的答案相互矛盾?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多